<?php
class Car extends Model { } class Color extends Model { } $vehicleColors = Car::find(5)->colors(); ?>what find prints
write a relation ship[
// Car.php
use Illuminate\Database\Eloquent\Model;
class Car extends Model
{
public function colors()
{
return $this->hasMany(Color::class);
}
}
// Color.php
use Illuminate\Database\Eloquent\Model;
class Color extends Model
{
public function car()
{
return $this->belongsTo(Car::class);
}
}
// Retrieve a car with id 5 and eager load its colors
$car = Car::with('colors')->find(5);
// Access the colors related to the car
$vehicleColors = $car->colors;
No comments:
Post a Comment