现在我在我的页面上显示视频,一旦我点击它,我就会被重定向到/ video {video} / view(其中{video}是视频ID)。
@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>
<div class="embed-responsive embed-responsive-16by9">
<iframe width="320" height="250" class="embed-responsive-item"
src="https://www.youtube.com/embed/{{ $video->video_url }}" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" msallowfullscreen="msallowfullscreen" oallowfullscreen="oallowfullscreen" webkitallowfullscreen="webkitallowfullscreen">
</iframe>
</div>
<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
我在我的数据库中创建了帖子
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->text('body');
$table->integer('user_id');
$table->integer('video_id');
});
但问题是我不知道如何在不同的视频上显示不同的帖子,我收到了user_id,但我无法获取video_id。
发布创建功能:
public function postCreatePost(Request $request) {
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
答案 0 :(得分:0)
查看文档https://laravel.com/docs/5.2/eloquent-relationships
你要创造的东西称为关系,我假设它将是一对一的视频和帖子,然后是一对一的用户以及视频和帖子。
我不确定你是如何得到用户的。但是,首先要做的是定义外键。
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->text('body');
$table->integer('user_id');
$table->integer('video_id');
$table->foreign('user_id')
->references('id')->on('users');
$table->foreign('video_id')
->references('id')->on('videos');
});
您还要在其他表上应用外键。无论如何,我假设这两个相关的表是users
和videos
。接下来,应用这些关系。
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the post associated by the user.
*/
public function posts()
{
return $this->hasMany('App\Post');
}
}
同样,我假设你在App
名称空间下。然后是帖子。
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function video()
{
return $this->hasOne('App\Video');
}
}
最后,视频。
class Video extends Model
{
public function post()
{
return $this->belongsTo('App\Post');
}
}
我重复我的第一个假设。 你可能会在帖子和视频上一对一,然后在用户和帖子上有一对一。这意味着,用户可以有很多帖子,帖子将会有一个视频。
控制器,如果我的假设在这一点上是正确的,它可能看起来像这样。
//You might want to use route model binding instead
//@see https://laravel.com/docs/5.2/routing#route-model-binding
public function showPost(Request $request, $post) {
$post = \App\Post::where('id', $post)->with('video')->get();
return view('template.post', ['post' => $post]);
}
public function showVideo(Request $request, $video) {
$video = \App\Video::where('id', $video)->with('post')->get();
return view('template.video', ['video' => $video]);
}
public function postCreatePost(Request $request)
{
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
$video = new Video();
$video->id = $request['video'];
$post->video()->save($video);
return redirect()->route('dashboard');
}
答案 1 :(得分:0)
我必须为我发布的路线提供ID,然后$ post-&gt; video() - &gt; associate($ video_id);我的视频ID。