首先,我刚开始使用laravel和整体使用php。现在我遇到了一个问题,我不知道如何从我的数据库中显示特定的视频。我的用户模型:
class User extends Model implements Authenticatable{
use \Illuminate\Auth\Authenticatable;
public function videos() {
return $this->hasMany('App\Video');
}
}
我的视频模型:
class Video extends Model{
public function user() {
return $this->belongsTo('App\User');
}
}
当我浏览我的视频以在仪表板中全部显示时,一切顺利:
<div class="row" id="features">
@foreach($videos as $video)
<div class="col-sm-4 feature">
<div class="panel">
<div class="panel-heading">
<h3 class="panel-title video_name">{{ $video->video_name }}</h3>
</div>
<iframe width="320" height="250"
src="https://www.youtube.com/embed/{{ $video->video_url }}" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" msallowfullscreen="msallowfullscreen" oallowfullscreen="oallowfullscreen" webkitallowfullscreen="webkitallowfullscreen">
</iframe>
<div class="info">
<p>Posted by {{ $video->user->first_name }} on {{ $video->created_at }}</p>
<hr class="postInfo">
</div>
<p>{{ $video->description }} </p>
<a href="{{ route('view.video', [$video->id]) }}" class="btn btn-danger btn-block">Continue to video</a>
</div>
</div>
@endforeach
</div>
但此时:
<a href="{{ route('view.video', [$video->id]) }}" class="btn btn-danger btn-block">Continue to video</a>
我打开新路线(http://localhost:8000/video/11/view),在这种情况下我想要显示ID 11等于我的video_url的视频
视频表格代码:
public function up(){
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->text('video_name');
$table->text('video_url');
$table->text('description');
$table->integer('user_id');
});
}
路线:
Route::get('/video/{video_id}/view', [
'uses' => 'SignInController@ViewVideo',
'as' => 'view.video']);
答案 0 :(得分:0)
将路线更改为以下..
Route::get('/video/{video}/view', [
'uses' => 'SignInController@ViewVideo',
'as' => 'view.video']);
在控制器..
public function ViewVideo(Video $video){
//any authorization logic...
return view('whatever',compact('video'));
}
答案 1 :(得分:0)
从Laravel 5.2开始,有一个名为Implicit Route Model Binding
的内容你可以阅读有关它的文档here。
所以在你的例子中。您可以像这样更改路线:
Route::get('/video/{video}/view', [
'uses' => 'SignInController@ViewVideo',
'as' => 'view.video'
]);
在您的Video
控制器的查看方法中:
public functin view(App\Video $video) {
// Your logic
}