title: storing id in the immediary table (latest not found)
body: <p>I have many to many relationships between <strong>posts</strong> and <strong>tags</strong> </p>
  
  <p>The problem is when I want to get the post id and the tag id to link them in their immediary table I did a simple query to do that and I used <strong>latest()</strong> method in laravel query builder and it threw this error:</p>
  
  <blockquote>
    <p>Method Illuminate\Support\Collection::latest does not exist.</p>
  </blockquote>
  
  <p>This is my store function: </p>
  
  <pre><code>  public function store(Request $request)
    {
      {
        $this-&gt;validate($request,[
          'title'=&gt;'required',
          'body'=&gt;'required',
          'tags'=&gt;'required',
          'slug'=&gt;'required',
  
        ]);
  
        $input = $request-&gt;all();
          $tags = explode(",", $request-&gt;tags);
  
            if ($input != null) {
              $post = post::create($input);
              $post-&gt;tag($tags);
              // the problem is in this two lines
              $post_id = DB::table('posts')-&gt;pluck('id')-&gt;latest();
              $tagged_id = DB::table('tagging_tagged')-&gt;pluck('id')-&gt;latest();
              //
              dd($tagged_id , $post_id);
              $relationship = new post_tagged;
              $relationship-&gt;post_id = $post_id;
              $relationship-&gt;tagged_id = $tagged_id;
              $relationship-&gt;save();
              return redirect('/admin/blog')-&gt;with('success' , 'Post added successfully');
            }
            return redirect('/admin/blog');
      }
   }
  </code></pre>
  
  <p>the idea that I want to achieve with those two lines is to get the latest id in the post table and in the tags table (which is the newest post)
  and then storing it in the immediary table</p>
  
  <p><strong><em>I solved the problem by doing this</em></strong></p>
  
  <p><strong>store function</strong> </p>
  
  <pre><code>  public function store(Request $request)
    {
      {
        $this-&gt;validate($request,[
          'title'=&gt;'required',
          'body'=&gt;'required',
          'tags'=&gt;'required',
          'slug'=&gt;'required',
  
        ]);
  
        $input = $request-&gt;all();
          $tags = explode(",", $request-&gt;tags);
  
            if ($input != null) {
              $post = post::create($input);
              $post-&gt;tag($tags);
              $post_id = DB::table('tagging_tagged')-&gt;latest('taggable_id')-&gt;first();
              $post_id = $post_id-&gt;taggable_id;
              $tagged_id = DB::table('tagging_tagged')-&gt;latest('id')-&gt;first();
              $tagged_id = $tagged_id-&gt;id;
              $relationship = new post_tagged;
              $relationship-&gt;post_id = $post_id;
              $relationship-&gt;tagged_id = $tagged_id;
              $relationship-&gt;save();
              return redirect('/admin/blog')-&gt;with('success' , 'Post added successfully');
            }
            return redirect('/admin/blog');
           }
  </code></pre>