mercredi 30 décembre 2015

Laralvel 5: Model object could call Builder method. How does Laravel do that?

In the Laravel package, the model User is derived from Model class. Although Model class does not have "join" method, I could still use join method on the user object.

I am not clear how does Laravel do that.

$user = Users::find($id);
$user->
    join('GroupMember', 'GroupMember.UserID', '=', 'users.id')->
    join('Groups', 'GroupMember.GroupID', '=', 'Groups.GroupID')->
    where('users.id','=', $user->id)->get();

The above code is to get all related groups of the user. Because the $user is derived from Model class, but the Model class dos not have "join" method...

Here is the User definition.

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;


class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire