I can't find the problem in this case and need some help with Laravel Many to Many Relationship:
lluminate\Database\Eloquent\RelationNotFoundException
Call to undefined relationship [registered_events] on model [App\User].
Whenever I try to access the relation the above error shows, eventhough the relation seems correct. The inverse relation works fine.
User Model
class User extends Authenticatable
{
public function registered_events()
{
return $this->belongsToMany(Event::class, 'event_user', 'user_id', 'event_id' )->withPivot('id', 'status', 'eventmanager', 'created_at', 'updated_at');
}
}
Event Model
class Event extends Model
{
public function registered_users()
{
return $this->belongsToMany(User::class, 'event_user', 'event_id', 'user_id')->withPivot('id', 'status', 'eventmanager', 'created_at', 'updated_at');
}
}
EventAllocationController
public function admin_event_overview()
{
$user = auth()->user();
// Problem here ⬇️
$eventmanagers = $user->with('registered_events')->get();
return view('dashboard.admin.event_allocation.overview', compact('user','eventmanagers'));
}
Related
I have 2 models, 'Tickets' and 'Messages', user can have many tickets and each ticket many messages.
class Message extends Model {
public function Ticket() {
return $this->belongsTo(Ticket::class, 'ticket_id', 'ticket_id');
}
public function user() {
return $this->belongsTo(User::class, 'user_id', 'user_id');
}
}
class Ticket extends Model {
public function messages() {
return $this->hasMany(Message::class, 'ticket_id', 'ticket_id');
}
public function user() {
return $this->belongsTo(User::class, 'user_id', 'user_id');
}
}
When trying to create a message attached to current user via the below code:
$Ticket->messages()->create([
'message' => $post['message']
]);
I get the following error:
Cannot insert the value NULL into column 'user_id', table 'messages'
Since Ticket is already linked to a user, I assumed it's going to cascade to message as well. I can manually specify it but I want everything to be built using laravel relationships the most correct way
Hi I simply want to get permissions of the role, I am trying following
$r = Role::find(1);
dd($r->permissions);
The above script does not return any permission however you can see there is data in the below tables. I also tried following but no luck
$role = Role::with('permissions')->where('id', 1)->first();
I have data in the table as you can see
Table:tes_permissions
Table: tes_roles
Table: tes_permission_role
And following are Models
class Permission extends Model
{
protected $table = 'tes_permissions'
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
And
class Role extends Model
{
protected $table = 'tes_roles';
public function permissions() {
return $this->belongsToMany('App\Permission', 'tes_permission_role', 'permission_id', 'role_id');
}
}
Can someone kindly guide me what can be the issue, I would appreciate.
You mixed the order of properties in the belongsToMany(). The third argument is specifying the ID for the model defining the relationship. So change to the following:
public function permissions() {
return $this->belongsToMany('App\Permission', 'tes_permission_role', 'role_id', 'permission_id');
}
And on the Permission model, also define it to be sure.
public function roles()
{
return $this->belongsToMany('App\Role', 'tes_permission_role', 'permission_id', 'role_id');
}
I'm implementing a simple user role and permissions model in Laravel, but I'm not sure if the Eloquent relationship I need exists. I want to return a relationship of User's permissions, but that is via the Role model. A user only has one Role via role_id, but a Role has many Permissions.
Users belongsTo => Role hasMany => Permissions
I want an Eloquent relationship method of User->permissions() but hasManyThrough is not the right relationship structure.
Any ideas?
like this.
User
public function permissions()
{
return $this->role->permissions;
}
You can use a HasManyThrough relationship:
class User extends Model
{
public function permissions()
{
return $this->hasManyThrough(
Permission::class, Role::class, 'id', null, 'role_id'
);
}
}
I assume you have model with name User.php Role.php Permission.php
then relationship define as per below
In User.php
public function role()
{
return $this->belongsTo(Role::class, 'role_id');
}
In Role.php
public function permissions()
{
return $this->hasMany(Permission::class, 'role_id');
}
Now use this relationship
public function show($id)
{
$user = User::select('id', 'name', 'email', 'role_id')
->with([
'role.permissions'
])
->find($id);
$permissions = $user->role->permissions;
dd($permissions); //here is your permission list
}
like in the headline is written, I'm getting a :
No query results for model [App\Models\Thread\Comment].
Error message. I'm getting this error after I'm trying to delete a Thread. The funny thing is that this Error Message says "No results for the Comment Model. But I wan't to delete a thread, not a comment. It doesn't matter if the Thread have some comments or not, I'm getting this error message every time I try it. I can't really say why, cause I haven't changed the Comment Model. Can someone have a look over it?
My Comment model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public $table = 'comments';
public $fillable = [
'comment',
'thread_id',
'user_id',
];
public function commentuser()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
}
Thread Model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model
{
public $table = 'thread';
public $fillable = [
'thread',
'content',
'user_id',
'themen_id',
];
public function userthread()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
public function threadthema()
{
return $this->belongsTo('App\Thread\Thema', 'thema_id', 'id');
}
}
The delete route:
Route::delete('/show/{id}', ['as' => 'destroycomment', 'uses' => 'Test\\TestController#destroycomment']);
The blade where the threads + the comments are:
http://pastebin.com/0NUPx18C
distroy method in controller:
public function destroy($id)
{
$thread = Thread::query()->findOrFail($id);
$thread->delete();
return redirect(action('Test\\TestController#startpage', [Auth::user()->id]));
}
I am struggling with this for a while now, but I can't figure it out how it works.
In laravel I have a few models with relationships. I wan't to have al the accounts based on the logged in user and the passed parameter for the workspace.
This is how the models looks like: (I only coppied the methods to keep it short)
The user Model:
class User extends Eloquent implements UserInterface, RemindableInterface {
public function workspaces()
{
return $this->hasMany('Workspace', 'user_id');
}
public function account()
{
return $this->hasManyThrough('account', 'Workspace', 'id', 'workspace_id');
}
}
The workspace model:
class Workspace extends Eloquent implements UserInterface, RemindableInterface {
public function account()
{
return $this->hasMany('account', 'workspace_id', 'id');
}
public function user()
{
return $this->belongsTo('User', 'user_id', 'id');
}
}
The account model
class account extends Eloquent implements UserInterface, RemindableInterface {
public function account_url()
{
return $this->hasOne('acountUrl', 'id', 'account_url_id');
}
public function workspace()
{
return $this->belongsTo('Workspace', 'workspace_id', 'id');
}
}
The account_url model
class account_url extends \Eloquent implements UserInterface, RemindableInterface {
public function account()
{
return $this->belongsToMany('account', 'id', 'account_url_id');
}
}
So I want from the logged-in user with a specific workspace all the account with the account_urls
something like this: user->workspace->account->account_url
I tried the following things but it don't work:
$account_urls = user::find( Auth::user()->id)->first()->workspaces()->where('id', '=', 1)->account()->account_url()->select('url')->get();
and:
$account_urls = account::where('workspace_id', '=', '1')->account_url()->select('url')->get();
Only when I do it like this:
$account_urls = account::find(1)->account_url()->select('url')->get();
But then I get only 1 url, but when I replase find(1) for all() I get an error?
Is there someone who can help me with this?
Tanks,
Your relations are wrong, change them to:
// User
public function account()
{
return $this->hasManyThrough('Account', 'Workspace', 'user_id', 'workspace_id');
}
// Account
// use camelCase for relations
public function accountUrl()
{
// I assume you have account_url_id on accounts table
// If it's opposite, then use hasOne
return $this->belongsTo('AcountUrl', 'account_url_id', 'id');
}
// AccountUrl (use camelCase)
public function account()
{
// if above is hasOne, then here belongsTo instead.
return $this->hasOne('account', 'account_url_id', 'id');
}
Now, fetching models:
// this part is .. amazing ;)
user::find( Auth::user()->id )->first();
// it does this:
Auth::user()->id // fetch user and get his id
user::find( .. ) // fetch user with given id, you have this user already above...
->first() // fetch first row from users table (not the one with id provided before)
so you want:
$account_urls = Auth::user()->workspaces()
->where('id', '=', 1)->first() // first fetches the result
// or simply
// ->find(1)
->accounts()->first()->accountUrl()
->pluck('url'); // this does 'SELECT url' and returns only this field instead of model
Just remember that:
$user->workspaces
$workspace->accounts
these are collections, so you can't call anything of the model on them, you need to get single model first.