How to select foreign key value in Laravel
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);
}
}
Related videos on Youtube
usertest
Updated on June 04, 2022Comments
-
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 over 6 yearsyour class car should be
Car
-
usertest over 6 yearsWhat about the query Any mistakes?
-
Jigar Shah over 6 yearsI'm confused with your tables, in query you have
cars
andvehicles
while models havecar_category
andvehicles
-
usertest over 6 years
cars
andvehicles
are function names of relationship models.
-
-
usertest over 6 years
Class 'Vehicle' not found
in HasRelationships.php (line 487) -
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 over 6 yearsThanks,But what should be the query?
-
Bivin over 6 yearsTo get the Car along with their Vehicle information you can do a query using Eager Loading $result = Car::with('vehicles')->get();
-
usertest over 6 yearsOk and get vehicle table's data using
$result->vehicles->car_type;
Is n't it? -
usertest over 6 yearsOk.How to get the table data in view blade.By
$result->vehicles->car_type;
Right? -
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 over 6 yearsWhen I dd the result it gives only the car table data
-
usertest over 6 yearsIn view displays the error :
Property [id] does not exist on this collection instance. (View: C:\xampp\htdocs\laravel\movecab\resources\views\vehicles\vehicle.blade.php)
-
usertest over 6 yearsCan you give me a sample example?