lundi 4 décembre 2017

How to select foreign key value in laravel

I've two tables one is car_category having the fields - id,type. Another table named vehicle having field - c_id(FK Refers car - id). Now I want to display the FK(c_id) value which is car-type. I've the below code in models,

class Car extends Model

{
protected $guarded = [];
protected $table = 'car_category';

 public function vehicles()
 {
return $this->hasMany('Vehicle');
 }
}

vehicle model,

 class Vehicle extends Model
 {
  protected $guarded = [];
  protected $table = 'vehicles';
     public function cars()
   {
    return $this->belongsTo('Car');
   }
 }

What 'll be my query for this?I've tried this code,results error.

        $vehicles = "SELECT cars.cartype,vehicles.model FROM cars,vehicles 
        WHERE cars.id = vehicles.c_id";

How can I achieve this?Can anybody help me?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire