dongpaozhi5734 2019-06-18 01:18
浏览 18
已采纳

在控制器Laravel 5.8.22中使用URL中的参数

I'm trying to pass anINT from this URL: myapp.build/courses/anINT (implemented in the CoursesController) to $id in the Lesson_unitsController function below. I've tried a lot of solutions, but I can't seem to get it right.

The function in the CoursesController which implements the url is:

    public function show($id)
{
    $course = Course::find($id);
    return view('courses.show')->with('course', $course);
}

Part of the show.blade.php file is:

@if(!Auth::guest())
    @if(Auth::user()->id == $course->user_id)
        <a href="/courses/{{$course->id}}/edit" class="btn btn-outline-secondary btn-light">Edit Course</a>
        <a href="/lesson_units/specificindex" class="btn btn-outline-secondary btn-light">Lesson Units</a>

        {!!Form::open(['action'=> ['CoursesController@destroy', $course->id], 'method' => 'POST', 'class' => 'float-right'])!!}
            {{Form::hidden('_method', 'DELETE')}}
            {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
        {!!Form::close()!!}
    @endif
@endif

The Lesson_unitsController functions are:

    public function index()
{
    $lesson_units = Lesson_unit::orderBy('title','asc')->paginate(10);
    return view('lesson_units.index')->with('lesson_units', $lesson_units);
}

public function specificindex($id)
{
    $course = Course::find($id);
    return view('lesson_units.specificindex')->with('lesson_units', $course->lesson_units);
}

And the specificindex.blade.php file is:

@extends('layouts.app')
@section('content')
<div class="container">
  <div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">Dashboard</div>
            <div class="card-body">
                <a href="/lesson_units/create" class="btn btn-primary">Create lesson unit</a>
                <p>
                <h3>Your lesson_units</h3>
                @if(count($lesson_units) > 0)
                    <table class="table table-striped">
                        <tr><th>Title</th><th></th><th></th></tr>
                        @foreach($lesson_units as $lesson_unit)
                            <tr><td>{{$lesson_unit->title}}</td>
                                <td><a href="/lesson_units/{{$lesson_unit->id}}/edit" class="btn btn-outline-secondary btn-light">Edit</a></td>
                                <td>
                                    {!!Form::open(['action'=> ['Lesson_unitsController@destroy', $lesson_unit->id], 'method' => 'POST', 'class' => 'float-right'])!!}
                                    {{Form::hidden('_method', 'DELETE')}}
                                    {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
                                    {!!Form::close()!!}
                                </td>
                            </tr>
                        @endforeach
                    </table>
                @else
                    <p>You have no lesson unit.</p>
                @endif
                @if (session('status'))
                    <div class="alert alert-success" role="alert">
                        {{ session('status') }}
                    </div>
                @endif
                You are logged in!
            </div> </div> </div> </div> </div>

@endsection

The routes in web.php are:

Route::resource('courses', 'CoursesController');
Route::resource('lesson_units', 'Lesson_unitsController');
Route::get('/courses/{id}', 'Lesson_unitsController@specificIndex');

I want that when the link for Lesson Units is clicked on the page, the id in the url is passed to the specificindex function in the Lesson_unitsController. Now, I get just a blank page. What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dongyan4424 2019-06-18 02:41
    关注

    Try to understand the concept of RESTful and CRUD.

    By using Route::resource('courses', 'CoursesController');, Laravel has helped you to register the following routes:

    Route::get('courses', 'CoursesController@index');
    Route::get('courses/create', 'CoursesController@create');
    Route::post('courses/{course}', 'CoursesController@store');
    Route::get('courses/{course}/edit', 'CoursesController@edit');
    Route::put('courses/{course}', 'CoursesController@update');
    Route::delete('courses/{course}', 'CoursesController@destroy');
    

    Then, when you make GET request to myapp.build/courses/123, Laravel will pass the request to the show function of your CoursesController like:

    public function show(Course $course)
    {
     return view('lesson_units.index')->with('lesson_units', $course->lesson_units);
    }
    

    Laravel will automatically resolve the Course from your database using the parameter passed into the route myapp.build/courses/{course}.

    Note: The variable name $course has to match with the one specify in route /{course}.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?