我正尝试从以下anINT
函数的URL:myapp.build/courses/anINT
(在CoursesController
中实现)传递$id
到Lesson_unitsController
。我尝试了很多解决方案,但似乎无法正确解决。
CoursesController中实现url的函数是:
public function show($id)
{
$course = Course::find($id);
return view('courses.show')->with('course', $course);
}
show.blade.php文件的一部分是:
@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
Lesson_unitsController
函数是:
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);
}
specificindex.blade.php
文件是:
@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
web.php中的路由为:
Route::resource('courses', 'CoursesController');
Route::resource('lesson_units', 'Lesson_unitsController');
Route::get('/courses/{id}', 'Lesson_unitsController@specificIndex');
我希望在页面上单击Lesson Units
的链接时,URL中的id
被传递到specificindex
中的Lesson_unitsController
函数。现在,我只有空白页。我在做什么错了?
答案 0 :(得分:1)
您没有设置处理进入$id
的路由。 Route 类中的资源方法将提供 GET 路由您的Lesson_unitsController
控制器,不需要任何变量。这是默认的索引路由,默认情况下不传递变量。
有两种方法可以做到这一点,但是最简单的方法就是为您的特定需求创建一条新路线:
Route::get('lesson_units/{id}', 'Lesson_unitsController@specificIndex');
然后使用传入变量在控制器中创建specificIndex
函数:
public function specialIndex($id)
{
$course = Course::find($id);
// return view to whatever you like
}
HTH
答案 1 :(得分:1)
尝试理解 RESTful 和 CRUD 的概念。
Laravel通过使用 $name = DB::connection('conn_name')->table('employee')->select(DB::connection('conn_name')->raw("encode(decrypt(name, 'key', 'aes'), 'escape')"))->get();
帮助您注册了以下路线:
Route::resource('courses', 'CoursesController');
然后,当您向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');
进行 GET 请求时,Laravel会将请求传递给myapp.build/courses/123
的{{1}}函数,例如:
show
Laravel将使用传递到路由CoursesController
的参数从数据库中automatically resolve public function show(Course $course)
{
return view('lesson_units.index')->with('lesson_units', $course->lesson_units);
}
。
注意:变量名Course
必须与路由myapp.build/courses/{course}
中指定的变量名匹配。