Skip to content
Advertisement

Tag: eloquent

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

How to make relationship between three table in laravel eloquent

I have three tables, Sections,Questions and options. Each Section has many questions and each question has many options. How can I fetch data from this tables related to each other? Section model relationship with Question model Question model relationship with Option model: my query: It returns this: I want each questions member contains options array related to it. Answer You

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 update model’s one-to-many relation’s items

I have two Eloquent models: Item’s columns are id, userId and name. When I want to update the user (PUT /users/<id>) I also want to update the items in the same request, meaning: Skip existing items Add new items Remove extra items Afterwards both DB and the relation should have up-to-date items. Example of the request data: I tried to

How do I create a eloquent Polymorphic relation

I have a two eloquent models model User and Retailer. I want to create a new model “Task” where there there should be a field “added_by” which can denote either a user or a retailer. How can I create the migration for Task so that there is a field that can denote either user or retailer? How can I create

Advertisement