Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Pivot table "3rd Model relationship" in Laravel Eloquent

I have this Many-to-Many relation Laravel Eloquent relationship The Models are:

class Email extends Model //actually represent the email account
{
    protected $table = 'emails';

    protected $fillable = [
        'user_id',
        'name',
    ];

    public function messages() {
       return $this->belongsToMany(Message::class)->withPivot('email_subtype_id');
    }
}

class Message extends Model //actually represent the email message
{
    protected $table = 'messages';

    protected $fillable = [
        'subject',
        'body ',
    ];

    public function emails() {
        return $this->belongsToMany(Email::class)->withPivot('email_subtype_id');
    }
}

class EmailMessage extends Pivot //actually represent the pivot table
{
    protected $table = 'email_message';

    protected $fillable = [
        'email_id',
        'message_id',
        'email_subtype_id',
    ];

    public function email() {
        return $this->belongsTo(Email::class);
    }

    public function message() {
        return $this->belongsTo(Message::class);
    }

    //this is the relation to a third model called EmailSubtype
    //I want to include this relation to the Pivot when using it
    public function subtype() {
        return $this->belongsTo(EmailSubtype::class, 'email_subtype_id');
    }
}

class EmailSubtype extends Model //3rd Model need to be included with Pivot
{
    protected $table = 'email_subtypes';

    protected $fillable = [
        'name'
    ];

    public function pivotEmailSubtype(){
        return $this->hasMany(Pivot::class, 'email_subtype_id');
    }
}

I am able to do this in the Controller:

    $email = Email::find(1);

    foreach($email->messages as $message) {
        $subtype_id = $message->pivot->email_subtype_id;
        dd($subtype_id); //1 that relates to subtype: CC Email Account
        //also I can get the name indirectly away from the relation as follows:
        $subtypeName = EmailSubtype::find($subtype_id)->first()->name;
        dd($subtypeName);
    }

Here I am getting the email_subtype_id only by direct pivot relation but have to do extra work to get the related email sub-type name.

I need to get the email sub-type name [Directly] from the 3rd Model EmailSubtype relationship oneToMany [hasMany and belongsTo] that relates to Pivot model with EmailSubtype model using something like:

$message->pivot->subtypeName;

Any help please!

like image 886
WillKoz Avatar asked Jun 02 '18 21:06

WillKoz


People also ask

What is the default name of pivot table in Laravel?

Laravel has a default naming scheme for pivot tables which will make it easier for us to manage. Laravel expects a pivot table to be named tableA_tableB where in our case, tableA and tableB are games and users.

Is Laravel’s eloquent one-to-one?

BUT . . . we’ve told Laravel that it’s a one-to-one relationship, so, the result is a single Eloquent instance. Notice how the same thing happens when accessing the inverse relationship:

How does Laravel determine the relationship name of a model?

By default, Laravel will determine the relationship associated with the given model based on the class name of the model; however, you may specify the relationship name manually by providing it as the second argument to the whereBelongsTo method:

What is a relational database in Laravel?

Based on all the terms we’ve discussed till now, we can finally talk about something that has a direct link to models in a web framework (Laravel) — relational databases. For most of us, the primary database used is MySQL, MariaDB, PostgreSQL, MSSQL, SQL Server, SQLite, or something along those lines.


1 Answers

Use this:

public function messages() {
   return $this->belongsToMany(Message::class)->withPivot('email_subtype_id') 
       ->using(EmailMessage::class);
}

$message->pivot->subtype->name
like image 114
Jonas Staudenmeir Avatar answered Sep 26 '22 00:09

Jonas Staudenmeir