title: How to query many to many relation tables in laravel?
body: <p>i am trying to query many to many relation for my get api call. i have three table as shown here but i am not using pivot table.</p>
  
  <p><a href="https://i.stack.imgur.com/6FYx2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6FYx2.png" alt="enter image description here"></a></p>
  
  <p>This is my Projects model class and this the function</p>
  
  <pre><code>  public function projectRewd()
     {
  
       return $this 
                 -&gt;belongsToMany('App\Rewards','rewards','project_id','reward_id');
  
  
     }
  </code></pre>
  
  <p>And this is my Rewards model class and function</p>
  
  <pre><code>public function projectShip()
      {
          return $this-&gt;belongsToMany('App\Shipping_location','shipping_location','projects_id','rewards_id');
  
      }
  </code></pre>
  
  <p>This is my api controller function</p>
  
  <pre><code>Route::get('projects/{id}', function($id) {
      $proj = Projects::whereHas('projectRewd', function($q)
      {       
          $q-&gt;where('id', $id);         
      });                       
      return $proj;
  
   });
  </code></pre>
  
  <p>i am using this link for api call</p>
  
  <pre><code>http://localhost:8000/api/projects/1
  </code></pre>
  
  <p>i want to extract rewards data and shipping_location data associate with project_id.</p>
  
  <p>i am getting this error</p>
  
  <pre><code>"message": "Object of class Illuminate\\Database\\Eloquent\\Builder could not be converted to string"
  </code></pre>
  
  <p>i check and tried all related error from different post.</p>
  
  <p>i also search and tried many technique. Cant solve my problem.
  Please suggest me how to do this??
  can i do this type of query in larvel without using pivot table??</p>