I am using a left join
to combine two tables based on a common column but I seem to lose the primary key id
in my left table. Every other column is returned
My controller:
$prod_manu = $request->prod_manu;
return $data = Product::leftJoin('manufacturers','products.Manufacturer_id','=','manufacturers.id')
->where('products.Manufacturer_id',$prod_manu)
->get();
Product Model:
class Product extends Model{
public function types(){
return $this->belongsTo('App\types','Type_id');
}
public function manufacturers(){
return $this->belongsTo('App\manufacturers','Manufacturer_id');
}
}
This is probably because both Product
and manufacturers
have an id
column, so one of them gets overwritten.
Instead of using the left join, you can use eloquent's with
method.
If you have your relationships and class structure setup properly you should be able to access the Manufacturer
's products like this:
$manufacturer = Manufacturer::where('id', $prod_manu)->with('products')->get();
return $manufacturer->products;
Also note that class names should always be singular and start with a capital letter in laravel. App\manufacturers
Shouldn't be valid. Please read the article I linked above about setting up your eloquent relationships.