relationship

Many to Many to MorphToMany Relationships

自古美人都是妖i 提交于 2021-01-29 13:42:47
问题 I have an Exam , Question and Tag (Spatie/laravel-tags package) models. An Exam consists of many Questions (Many to Many), and Question has many Tags (MorphToMany). I would like to have a method on the Exam model to get all the tags of Exam through its associated questions, such that $exams→tags() returns all the tags from associated questions belonging to the exam. Can anyone point me towards what may be the best course to take in order to achieve this? 回答1: If you have proper described

Laravel relationship based on custom attribute not working both directions

て烟熏妆下的殇ゞ 提交于 2021-01-29 10:53:33
问题 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() { return $this->hasMany(Product::class, 'category_id', 'id'); } If I use this relationship

Laravel hasone relationship

与世无争的帅哥 提交于 2021-01-29 09:16:20
问题 I am trying to use hasone relation but getting null error. below is my code User Model: function profile() { $this->hasOne('App\Profile'); } Profile Model: function User() { return $this->belongsTo('App\User'); } Register Controller protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); if($user) { $profile = new Profile(); $profile->name = 'Fake Name'; $profile->father = 'Fake Father

Laravel hasone relationship

久未见 提交于 2021-01-29 09:09:36
问题 I am trying to use hasone relation but getting null error. below is my code User Model: function profile() { $this->hasOne('App\Profile'); } Profile Model: function User() { return $this->belongsTo('App\User'); } Register Controller protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); if($user) { $profile = new Profile(); $profile->name = 'Fake Name'; $profile->father = 'Fake Father

Laravel API JSON customization and table relationship

允我心安 提交于 2021-01-29 08:09:08
问题 I'm developing a social media project using Laravel. I need to provide a customized API for Follows and Followers. Accordingly, I want to establish a relationship between the user table and the followers and followed tables. I have looked at a lot of resources but get confused. Can anyone help me with relationships? Models Users: id, username, email, password Follows: id, user_id, follow_id(user_id) Followers: id, user_id, follower_id(user_id) API Url: http://localhost/api/follows/user_id/1

Limiting the result of nested relationship in Laravel

末鹿安然 提交于 2021-01-29 05:12:15
问题 I have a nested relationship that I want to set a condition and a limit. $data = Accommodation::with('accommodationFacilities', 'city') ->take(10) ->with('accommodationRooms.roomPricingHistory') ->limit(2) ->where('is_deleted', 0) ->paginate(10); Now I have a nested relation on line 2 that I want to limit this relation by: roomPricingHistory . The code limits the parent relation which is: accommodationRooms . Any idea how I can limit the child relation and how can I set an if for it so if a

Limiting the result of nested relationship in Laravel

末鹿安然 提交于 2021-01-29 05:10:48
问题 I have a nested relationship that I want to set a condition and a limit. $data = Accommodation::with('accommodationFacilities', 'city') ->take(10) ->with('accommodationRooms.roomPricingHistory') ->limit(2) ->where('is_deleted', 0) ->paginate(10); Now I have a nested relation on line 2 that I want to limit this relation by: roomPricingHistory . The code limits the parent relation which is: accommodationRooms . Any idea how I can limit the child relation and how can I set an if for it so if a

Laravel Bulk Detach

ぐ巨炮叔叔 提交于 2021-01-29 04:41:33
问题 First, I use detach() in laravel with this approach and this is work! $student = \App\StudentRegistrars::withTrashed()->findOrFail($id); $student->father_registrars()->detach(); $student->mother_registrars()->detach(); But when I am trying to mass detach in laravel, I can't do that. $students = \App\StudentRegistrars::whereIn('id', $ids); $student->father_registrars()->detach(); $student->mother_registrars()->detach(); Any solution about this problem? 回答1: There's quite a few issues with your

Error in creating a calculated column that refers to a linked table in Power Pivot

五迷三道 提交于 2021-01-28 10:36:09
问题 Context: I have a data model in Power Pivot with two tables, tTasks and tCaseworks. They are linked through the common field casework_id (unique to tCaseworks). I am attempting to create a calculated column in tCaseworks that checks whether or not that particular casework has any (i.e. one or more) corresponding tasks. I have attempted the following DAX code: has_task = IF ( CONTAINS ( RELATEDTABLE ( tTasks ); tTasks[casework_id]; tCaseworks[casework_id] ); "Yes"; "No" ) Problem: When I have

Collection Relationship Select columns not working Laravel

拈花ヽ惹草 提交于 2021-01-28 05:24:02
问题 I am trying to select columns (id, title) of the relationship database (product_detail) but it's not working at all. My Query: RoomProduct::select('product_id', DB::raw('SUM(value) as total')) ->whereHas('room.offer', function($sql) use ($offer_id) { $sql->where('id', $offer_id); })->whereHas('product_detail', function($sql) use ($category_id) { $sql->select("id", "title")->with(['category' => function ($query) { $query->where('parent_id', $category_id); }])->orWhere('id', $category_id); })-