lundi 13 novembre 2017

How to query many to many relation tables in laravel?

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.

enter image description here

This is my Projects model class and this the function

  public function projectRewd()
   {

     return $this 
               ->belongsToMany('App\Rewards','rewards','project_id','reward_id');


   }

And this is my Rewards model class and function

public function projectShip()
    {
        return $this->belongsToMany('App\Shipping_location','shipping_location','projects_id','rewards_id');

    }

This is my api controller function

Route::get('projects/{id}', function($id) {
    $proj = Projects::whereHas('projectRewd', function($q)
    {       
        $q->where('id', $id);         
    });                       
    return $proj;

 });

i am using this link for api call

http://localhost:8000/api/projects/1

i want to extract rewards data and shipping_location data associate with project_id.

i am getting this error

"message": "Object of class Illuminate\\Database\\Eloquent\\Builder could not be converted to string"

i check and tried all related error from different post.

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??



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mnpSKR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire