3
votes

Let's say i have this DB structure:

Table A:
-id
-user_id

Table B:
-id
-user_id

Table C:
-id
-user_id

Users:
-id

This is my User Model

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'users';
    protected $fillable = ['name','email','username','password'];

    /**
     * Relationships
     */

    public function tableA()
    {
        return $this->hasOne(\App\Models\TableA::class);
    }
    public function tableB()
    {
        return $this->hasOne(\App\Models\TableB::class);
    }
    public function tableC()
    {
        return $this->hasOne(\App\Models\TableC::class);
    }
    public function tableD()
    {
        return $this->hasOne(\App\Models\TableD::class);
    }

}

This is what i want to achieve:

public function tables()
{
    // return all records from all 3 tables (Table A, Table B, Table C)
}

The way I can get a certain information on a table is by defining a relationship within the User model, saying it hasOne record of TableA, TableB, etc. However, I'm searching for a way to find on which of these tables (a,b,c,d) there is a Fk of User, following the same concept of relationships.

Note: Polymorphism is not a solution in this case

2
what you have tried for this issue?Bhargav Chudasama
Please add some more context - especially, how is this question related to Laravel after all?Nico Haase
@BhargavChudasama until now, i'm making single relatioships and calling them, but i'm looking for a simple and efficient way to achieve this, an all in one of sort.JoseSilva
@NicoHaase its a laravel project with eloquent relations, why this not related with laravel? Should i tag it with node js?JoseSilva
If you tag a question with a specific technology, you should provide context for it. If the only problem is reading the foreign keys from an existing database, the only technology involved might be the database system. If I got you wrong, please add the source code that is involvedNico Haase

2 Answers

1
votes

Use an Eloquent local scope to achieve what you are trying to do: class User { public function scopeAllTables($query) { return $query->join('table_a', 'table_a.user_id', '=', 'users.id') ->join('table_b', 'table_b.user_id', '=', 'users.id') ->join('table_c', 'table_c.user_id', '=', 'users.id') ->select('users.*') ->addSelect('table_a.id AS table_a_id') ->addSelect('table_b.id AS table_b_id') ->addSelect('table_c.id AS table_c_id'); } }

And in your controller, you can call this like: $allTables = User::allTables()->get();

1
votes

If you want User records which have data in All other tables (i.e. TableA, TableA,etc. ), you can use below query as you already defined relationship :

$users = User::whereHas('tableA')
             ->whereHas('tableB')
             ->whereHas('tableC')
             ->get();

And if you want to access data of a related table you can use below query :

 $users = User::with('tableA','tableB','tableC')
              ->whereHas('tableA')
              ->whereHas('tableB')
              ->whereHas('tableC')
              ->get();