1

I'm new to laravel, right now I'm stuck on getting video_id on my posts table where I should make it equal to id on my videos table. posts table = https://i.stack.imgur.com/tVWBP.jpg videos table = https://i.stack.imgur.com/2j4YH.jpg Route: displaying video ID when i 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]);});

PostController:

public function postCreatePost(Request $request) {

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

Post view:

@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

I tried to edit video_id column on my posts table and change it equal to id of my video and it worked, but how can I automatically assign it once I create post on a specific video? Thank you.

Arthur
  • 149
  • 1
  • 11
  • You need to include foreign key of `video` in `post table`. `video_id` must be foreign key referencing `id` column in `video` table – jonju Aug 03 '16 at 19:35
  • Are you assigning `video` to `post` separately or are you suppose to include `video` while saving the `post` – jonju Aug 03 '16 at 19:41
  • after ->save(), `$post->video_id` should be the last inserted id in to that table (see: http://stackoverflow.com/a/21084888/2468160). -- So you can also do it like this `return redirect(/video/.$post->video_id);` – P_95 Aug 03 '16 at 19:53
  • `$post->id` should be the last inserted id. – jonju Aug 03 '16 at 20:03

2 Answers2

1

Looks like you missed a line code in

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();
}
jonju
  • 2,711
  • 1
  • 13
  • 19
  • I don't have input with name video_id in my view.. All I want to do is when I post something on my videos(similar to youtube) I want that post to be assign to it's video, so I can post something different on every video.( So if video id which i posted is 11, all posts on that video must have id of 11(video_id), if video id 12, all posts have id of 12(ofc in column video_id). – Arthur Aug 03 '16 at 19:54
  • then change `video_id` with whatever it's in your view that's storing the video's id. If there is none. then it's not possible to relate the post & the video. something must be there to keep the video id – jonju Aug 03 '16 at 20:00
0

You should do $post->video()->associate($video) as well