Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0
$o_data = Posts::with('group')->get();

You have to provide the method name that defines the relationship.

Last updated 1 year ago.
0

Thank You troydavisson. I'm just catching myself all the time that laravel is simple - and I just forget about simpliest solusions.

Thank You. :)

Last updated 1 year ago.
0

I'm glad it worked.

To elaborate a bit (and for anyone else that might stumble on this), if you look at your Posts model, there's no way for Eloquent to know what the Authors relationship is without it executing all of your methods (which would be unnecessary and potentially dangerous) since 'Authors' is just given as a string until that method executes and the relationship is resolved and built. So instead, you have to do the eager loading pointing to the code that tells Eloquent which relationship to load.

Last updated 1 year ago.
0

Troy Davisson, Thank you!.

0

I am getting this exception BadMethodCallException with message Call to undefine method Illuminate\Database\Query\Builder::assign() My user Class: class User extends Model implements AuthenticatableContract, AuthorizableContract,CanResetPasswordContract { use Authenticatable, Authorizable,CanResetPassword;

   protected $hidden = ['password', 'remember_token'];

public function roles(){

     return $this->belongsToMany(Role::class);
}

public function hasRole($role){

    if (is_string($role)) {
        return $this->roles->contains('name',$role);
    }
    return !!$role->intersect($this->roles)->count();
}
public function assign($role){
    if (is_string($role)) {
        
       return  $this->roles()->save(

            Role::whereName($role)->firstOrFail();
            );
    }
    return $this->roles()->save($role);
}

} My Role Class class Role extends Model { public function permissions(){

	return $this->belongsToMany(Permission::class);
}

public function assign(Permission $permission){

	return $this->permissions()->save($permission);
}

}

0

Sign in to participate in this thread!

Eventy

Your banner here too?

grupamak grupamak Joined 5 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.