How to select foreign key value in Laravel

15,439

Solution 1

Try this

class Car extends Model  
{
protected $guarded = [];
protected $table = 'car_category';

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

The vehicle model

 class Vehicle extends Model
 {
  protected $guarded = [];
  protected $table = 'vehicles';

  public function cars()
  {
    return $this->belongsTo(Car::class, 'c_id');
  }
 }

Eloquent determines the foreign key of the relationship based on the model name. In this case, the Car model is automatically assumed to have a car_id foreign key. If you wish to override this convention, you may pass a second argument to the method

https://laravel.com/docs/5.5/eloquent-relationships#one-to-one

To get the Car along with their Vehicle information you can do a query using Eager Loading

$result = Car::with('vehicles')->get();

Solution 2

To get the Car along with their Vehicle information you can do a query using Eager Loading

$result = Car::with('vehicles')->get();

One more correction you have specified class name as string literals without specifying FQN, relationships in models should be defined using fully qualified name

Car Model

class Car extends Model
{
    protected $guarded = [];
    protected $table = 'car_category';

    public function vehicles()
    {
        return $this->hasMany(\App\Models\Vehicle::class);
    }
}

Vehicle Model

class Vehicle extends Model
{
    protected $guarded = [];
    protected $table = 'vehicles';
    public function cars()
    {
        return $this->belongsTo(\App\Models\Car::class);
    }
}
Share:
15,439

Related videos on Youtube

usertest
Author by

usertest

Updated on June 04, 2022

Comments

  • usertest
    usertest almost 2 years

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

    • Jigar Shah
      Jigar Shah over 6 years
      your class car should be Car
    • usertest
      usertest over 6 years
      What about the query Any mistakes?
    • Jigar Shah
      Jigar Shah over 6 years
      I'm confused with your tables, in query you have cars and vehicles while models have car_category and vehicles
    • usertest
      usertest over 6 years
      cars and vehicles are function names of relationship models.
  • usertest
    usertest over 6 years
    Class 'Vehicle' not found in HasRelationships.php (line 487)
  • M Khalid Junaid
    M Khalid Junaid over 6 years
    @saranya I have udpated my answer also if you have used different namespaces for your models not App\Models then i suggest you to put correct namespaces in defining relationships
  • usertest
    usertest over 6 years
    Thanks,But what should be the query?
  • Bivin
    Bivin over 6 years
    To get the Car along with their Vehicle information you can do a query using Eager Loading $result = Car::with('vehicles')->get();
  • usertest
    usertest over 6 years
    Ok and get vehicle table's data using $result->vehicles->car_type; Is n't it?
  • usertest
    usertest over 6 years
    Ok.How to get the table data in view blade.By $result->vehicles->car_type; Right?
  • M Khalid Junaid
    M Khalid Junaid over 6 years
    @saranya yes pass it to view and then loop through your data and then loop through vehicles for each car
  • usertest
    usertest over 6 years
    When I dd the result it gives only the car table data
  • usertest
    usertest over 6 years
    In view displays the error :Property [id] does not exist on this collection instance. (View: C:\xampp\htdocs\laravel\movecab\resources\views\vehicles\veh‌​icle.blade.php)
  • usertest
    usertest over 6 years
    Can you give me a sample example?