maltekiefer's avatar

Get all data from two tables

Good morning:

I have a model that looks like this:

    protected $casts = [
        'id' => 'integer',
        'description' => 'string',
        'internalname' => 'string',
        'isactive' => 'boolean',
        'created_at' => 'datetime',
        'created_by' => 'string',
        'updated_at' => 'datetime',
        'updated_by' => 'string',
        'deleted_at' => 'datetime'
    ];

    public function communicationtypeCompanies()
    {
        return $this->hasMany('\App\Models\CommunicationtypeCompany', 'communicationtype_id', 'id');
    }

    public function communicationtypeContacts()
    {
        return $this->hasMany('\App\Models\CommunicationtypeContact', 'communicationtype_id', 'id');
    }

Now I tried this:

        $mail = \App\Models\Communicationtype::select('id')->where('icon', '=', 'envelope')->get();
        foreach($mail as $id){
            dd(\App\Models\CommunicationtypeContact::select('communicationdata')->where('communicationtype_id', '=', $id->id)->get());
            dd(\App\Models\CommunicationtypeCompanie::select('communicationdata')->where('communicationtype_id', '=', $id->id)->get());
        }

That works, but what I want to ask, are there a better a nicer way in Laravel to get the communicationdata from both tables that fetch the communicationtype_id from Communicationtype that have the icon envelope

0 likes
1 reply
mvd's avatar

Hi @maltekiefer

    foreach($mail as $id){
        dd($id->communicationtypeCompanies);
        dd($id->communicationtypeContacts);
    }

Please or to participate in this conversation.