Level 35
Do you refer to this ?
$event = Event::with('guests: function, arrival, departure')->first();
https://laravel.com/docs/10.x/eloquent-relationships#nested-eager-loading
Hi @all,
I have a many-to-many relation with additional pivot fields.
<?php
namespace App\Models;
use App\Events\EventCreated;
class Event extends BaseModel
{
/**
*
*
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany
*/
public function guests(): \Illuminate\Database\Eloquent\Relations\belongsToMany
{
return $this->belongsToMany(Guest::class)->using(GuestEvent::class)->withTimestamps();
}
}
The custom pivot model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\Pivot;
class GuestEvent extends Pivot
{
public function arrival(): HasOne
{
return $this->hasOne(Travelinformation::class);
}
}
How to add in the query the arrival? (->with('arrival') ) => eager loading
$event = Event::query()->first();
$data = $event->guests()->withPivot(['function', 'arrival', 'departure']);
Please or to participate in this conversation.