Category : relationship

I’m searching to load a relation inside a pivot table with "->with()". Table structure: Are 5 tables and Laravel Eloquent models user (id) team (id) role (id) team_user (id, team_id, user_id) team_user_role (id, team_user_id, role_id) class User extends Model { function teams(){ return $this->belongsToMany(Team::class) ->using(TeamUser::class); } } class Team extends Model { function users(){ return ..

Read more

I’m searching to load a relation inside a pivot table with "->with()". Table structure: Are 5 tables and Laravel Eloquent models user (id) team (id) role (id) team_user (id, team_id, user_id) team_user_role (id, team_user_id, role_id) class User extends Model { function teams(){ return $this->belongsToMany(Team::class) ->using(TeamUser::class); } } class Team extends Model { function users(){ return ..

Read more

public function show($id) { $post = Post::find($id); $comments = $post->comments()->orderBy(‘created_at’, ‘desc’)->get()->transform(function ($item, $key) { $item->registered = $item->created_at->diffForHumans(); return $item; }); return view(‘blog_views.post’)->with([‘post’ => $post, ‘comments’ => $comments]); } I want to add paginate(5) on $comments before return it to view and keep this values accessible in the view. {{ $comment->user->name }} relationship returning the creator ..

Read more

According to Laravel documentation OneToOne, OneToMany and ManyToMany relationships are available. https://laravel.com/docs/8.x/eloquent-relationships#many-to-many users id – integer name – string roles id – integer name – string role_user user_id – integer role_id – integer class User extends Model { public function roles() { return $this->belongsToMany(‘AppModelsRole’); } } Is it possible to change above many to many ..

Read more

I have tables like this products – id, name, description, category_id, brand_id, color_id, material_id, price extras – id, product_id, size_id, stock images – id, product_id, name, path size – id, name Model product : public function meta() { return $this->hasMany(‘AppMeta’, ‘product_id’, ‘id’); } public function image() { return $this->hasMany(‘AppImage’, ‘product_id’, ‘id’); } Model extra : ..

Read more

I have a table of Lessons. I want some Lessons to be prerequisites to other lessons. I call these lessons Goal and Prerequisite. I have made a Prerequisite model and Prerequisites table as below. I can retrieve Lesson->prerequisite using oneToMany in the Lesson model. I want to make the Prerequisite->Lesson relationship but can’t work out ..

Read more

I have a State, County, salesTeam, and user models. SalesTeam.php public function state(){ return $this->belongsTo(State::class); } public function manager(){ return $this->belongsTo(User::class,’manager_id’); } County.php public function counties(){ return $this->hasMany(‘AppCounty’)->orderBy(‘county’); } What I am trying to achieve is that there might be some cases where there is a submanager assigned to a salesTeam but only to a ..

Read more

I want to add to this call more results from usercontract table. public function showRelated() { return UserContract::with([‘topics’, ‘contracttax’, ‘contractproperty’, ‘persons’])->get(); } In my user model I have this relationship that gives me usercontracts in which a given user by email as been invited to. So I want to add the result from this relationship ..

Read more

I have a Usercontract table and model… and I can call the usercontracts with it’s relationships like this return UserContract ::with([‘contracttax’, ‘contractproperty’, ‘persons’, ‘contractwarranty’, ‘users’, ‘contracts’, ‘tags’]) ->orderBy(‘created_at’, ‘desc’)->get(); Then I have another user model with this method. public function nonAcceptedContracts() { return $this->belongsToMany(UserContract::class, ‘invite’, ’email’, ‘usercontract’, ’email’)->where(‘status’, ‘pending’); } Is there a way to ..

Read more

I want to recieve all users from a group. The problem is in this group their are student users (with a pivot table) and normal users. So i have to merge them together, but i still want to maintain all possibilities from eloquent. I came to this: dd($this->belongsToMany(User::class)->union($this->hasManyThrough(User::class,Student::class,’class_id’,’id’,’id’,’user_id’))->get()); But as result i get: My database ..

Read more

I’m trying to get users of entity using the relationship $users = $entity->users()->paginate(20); but now I want to run multiple where conditions depending on parameters sent to the API so if I do this $users = $entity->users(); //$users->where($where_array) $users->paginate(20); I get out of memory error. It’s getting all users first then tries to paginate (commented ..

Read more

Models Product and Category. Product model has this custom attribute: getCategoryIdAttribute(). because the way to get category id is a bit complex. With that attribute I can define the relationship (Product -> Category): public function category() { return $this->belongsTo(Category::class, ‘category_id’, ‘id’); } This relationship works fine! But now on the Category model: public function products() ..

Read more

I am getting this error Method IlluminateDatabaseEloquentCollection::belongsToMany does not exist. And I have imported belongsToMany into User Model use IlluminateDatabaseEloquentRelationsBelongsToMany; Am using laravel version 7.2.28 Where am I getting wrong? class User extends Eloquent { /** * @return IlluminateDatabaseEloquentRelationsBelongsToMany */ public function roles() { return $this->account->belongsToMany(‘NaviculaEntitiesRole’); } } Sourc..

Read more