Laravel如何让我的video_id列等于我的视频表的id?

时间:2016-08-03 19:31:10

标签: laravel laravel-5 laravel-5.1 laravel-5.2

我是laravel的新手,现在我一直坚持在我的帖子表上获取video_id,我应该在视频表上将其设为等于id。 posts table = http://imgur.com/a/al2Bv videos table = http://imgur.com/a/wCNoe路线:发布时显示视频ID:

Route::get('/test/{videoID}', function($videoID) {
$video = Video::where('id', $videoID)->firstOrFail();
$posts = Post::all();
return View::make('layouts.viewvideo')->with(['video' => $video, 'posts' => $posts]);});

PostController中:

public function postCreatePost(Request $request) {

    $post = new Post();
    $post->body = $request['body'];
    $request->user()->posts()->save($post);
    return redirect()->back();

发布视图:

@foreach($video->posts as $post)
        <article class="post" data-postid="{{ $post->id }}">
            <div class="postbody">
                <p>{{ $post->body }}</p>
            </div>
            <?php
            $test = preg_replace('/([^\s]{20})(?=[^\s])/', '$1'.'<wbr>', $post->body);
            ?>
            <div class="info">
                Posted by {{ $post->user->first_name }} on {{ $post->created_at }}
            </div>
        </article>
    @endforeach

我尝试在我的帖子表上编辑video_id列,并将其更改为等于我的视频的ID并且它有效,但是如果我在特定视频上创建帖子后如何自动分配?谢谢。

2 个答案:

答案 0 :(得分:1)

看起来您错过了

中的行代码
public function postCreatePost(Request $request) {
    $post = new Post();
    $post->body = $request['body'];
    $post->video_id = $request['video_id']; // this is what you missed. assuming you have input with name video_id in your view(blade)
    $request->user()->posts()->save($post);
    return redirect()->back();
}

答案 1 :(得分:0)

你应该做$ post-&gt; video() - &gt;关联($ video)