Laravel 如何让我的 video_id 列等于我的视频 table 的 ID?

Laravel how can I make my video_id column equal to id of my videos table?

我是 laravel 的新手,现在我坚持在我的 posts table 上获取 video_id,我应该使它等于 id在我的视频中 table。 posts table = http://imgur.com/a/al2Bv videos table = http://imgur.com/a/wCNoe 路由:显示视频ID当我 post it:

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]);});

Post控制器:

public function postCreatePost(Request $request) {

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

Post 视图:

@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])/', ''.'<wbr>', $post->body);
            ?>
            <div class="info">
                Posted by {{ $post->user->first_name }} on {{ $post->created_at }}
            </div>
        </article>
    @endforeach

我试图在我的 post 上编辑 video_id 列 table 并将其更改为等于我的视频的 ID 并且它起作用了,但是一旦我如何自动分配它在特定视频上创建 post?谢谢。

看起来你在

中漏掉了一行代码
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();
}

你也应该做 $post->video()->associate($video)