rayan
rayan

Reputation: 45

Join two Models using Eloquent in laravel 5.3

I have two tables.

users table:

 id (integer)
 name (string)
 email (string)
 phone (string)
 codeMeli (string)

and user_admin table:

userId (integer)
adminId (integer)

Note: adminid field keeps the user id that is admin of another user. And a user can be admin of multiple users or a user might have more than one admin. For example:

| id | name | email | phone | codeMeli |            | userid | adminid |
 ---- ------ ------- ------- ----------              -------- ---------
| 5  | mike | m@yaho| 12345 | 12345678 |            |   6    |    5    |
| 6  | sara | s@yaho| 54321 | 87654321 |            |   7    |    5    |
| 7  | joe  | j@yaho| 56421 | 65875234 |            |   7    |    8    |
| 8  | sag  | s@yaho| 57635 | 64616843 |

I tried this query using OueryBuilder and works well:

Users::select('id','name','email','codeMeli','phone')
       ->join('user_admin','user_admin.userid','=','users.id')
       ->whereRaw("name LIKE '%".$name."%' and email LIKE '%".$email."%' and codeMeli LIKE '%".$codeMeli."%' and phone LIKE '%".$phone."%' and user_admin.adminid=".$CurrentUSerID)
       ->get();

But I want to use Eloquent.

And this is My user Model:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    protected $table = 'users';
    public $timestamps = false;
    protected $fillable = ['name','phone','email','codeMeli','admin'];

    public function userAdmin()
    {
        return $this->belongsToMany('App\UserAdmin','id','userid');
    }
}

and user_admin Model:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class UserAdmin extends Model
{
    protected $table = 'user_admin';
    public $timestamps = false;
    protected $fillable = ['userid','adminid'];

    public function users()
    {
       return $this->belongsToMany('App\Users','id','adminid');
    }
}

How should I write this query using Eloquent? Am I using relationships in a correct method in Models? I think my Models should be edited but I don't know how. Thanks for your help.

Upvotes: 4

Views: 15539

Answers (1)

Farzad Pakzad
Farzad Pakzad

Reputation: 69

You can make a new Model as 'Admin' that is related to users table and use relationships like this:

users model:

class Users extends Model
{
    protected $table = 'users';
    public $timestamps = false;
    protected $fillable = ['name','phone','email','codeMeli','admin'];


    public function userAdmin()
    {
        return $this->belongsToMany('App\Admin', 'user_admin', 'adminid', 'userid');
    }
}

Admin model:

class Admin extends Model
{
    protected $table = 'users';
    public $timestamps = false;
    protected $fillable = ['name','phone','email','codeMeli','admin'];
    public function userAdmin()
    {
        return $this->belongsToMany('App\Users', 'user_admin', 'userid', 'adminid');
    }
}

and in your Controller:

$user = Users::find($CurrentUSerID)->userAdmin()->get();

Take a look at https://laravel.com/docs/5.3/eloquent-relationships#many-to-many

Upvotes: 3

Related Questions