I’ve got a Product and a Category models: In my controller, I’m retrieveing all products and wanted to return the category object the product belongs to in the response, so I’m doing: and my resource looks like: but the response I’m getting is: I tried loading the relationship differently: but the result is the same. It seems that the relationship
Tag: eloquent-relationship
laravel eloquent model record not fetched
I am facing a strange issue. When I do NumberConfiguration::all() I receive the following result: but in the database I have actually 2 record: I didnt find anything what can cause this problem, that not all record are represented by the eloquent model biding. I am using postgresql database. There is an observer in the parent class, but even if
Check user if is in chat room Laravel
I have models: ChatRoomMembers ChatRoom so I want to check if auth user is in chat room my relationships: ChatRoom: ChatRoomMembers: Answer So I created relationship in User Model: and I am getting only this user chat rooms
How to get foreign key from eloquent collection in laravel
I have hasMany relationship in my User model; I need to get a foreign id in Eloquent data Controller; Expected output; 1 How can I get the foreign key? Answer You can do the following. Since posts has hasmany relation so it return collection of object even though you have one item in posts. or If you still need one
Determine selected options based on many-to-many relationship
I have three tables: products, categories and category_product. In my edit form page I’m displaying the categories and subcategories values in a select box. The chosen category options are saved in the pivot table category_product. This table has product_id and category_id. Here’s my code. Product model Category model: edit product page view: subcats-select view How can I put “selected” attribute
Laravel filter relationship data
Code Data return Question: I tried to filtering data that if a relationship “getCauserDetails”, How can I not show the data if the relationship name is only found “test” or should I say null? So on the example output, it displays all the data, but I want to only the data that I filter just retrieve. Answer Because of your
Laravel Eloquent – Query with multiples parameters
I’m new to the Laravel framework and I could need a little help from you. Could someone help me to convert this request into Eloquent ? SELECT * FROM `non_working_days` WHERE YEAR(date) = “2021&…
How to deploy relationship in pivot of three model in laravel?
I’m developing a role and permissions based on laravel framework. I have 3 models : This is pivot table Now, a user can have a weblog with permission id 1,2 and another weblog with permission 1,2,3,4 How can I deploy relationships? and how can I check user permissions when managing a weblog. (middleware and …) Answer With the fact that
Laravel eloquent update column using relationship column
How can achieve this query? Sale::with([‘catalog’]) ->whereIn(‘id’, $ids) ->update([‘price’ => DB::raw(‘catalog.price’)]); This is not working, it shows undefined table… I tried to …
Laravel 5.5 How to get eloquent relationship count?
I have three tables as define below User id name Post id text user_id Comments id text post_id This is User model User.php class User { public function post() { return $this-&…