Skip to content
Advertisement

Tag: eloquent

Get all data where pivot id (Laravel)

Is there the best way/the simplest way to get all data where pivot? I tried this $article = Article::with(‘category’)->wherePivot(‘category_id’, $category)->get(); but i got error The relation is many to many Article id content Articles_Has_Categories id article_id category_id Category id name Answer Please try this:

How to establish a one-to-many relationship to a model whose type is stored in db

I have a database that looks like this: Database Graphical Representation The dance_performer.perfomer_type field hosts values such at ‘AppDancer’, ‘AppCouple’ or ‘AppFormation’. How would you proceed to connect the dance_performer.perfomer_id to the different models? I am not sure how I am supposed to write the relationships in the different models. Should I create a AppPerformer model which would then direct

Laravel seeder with Eloquent give timestamp null

I am trying to insert static data, with the help of the seeder and Eloquent as bellow. Even after using Eloquent i am getting timestamp null in database. Answer The timestamp columns (created_at and updated_at) will be assigned automatically only if you are using the Eloquent save() method and create method are Eloquent. While insert method is not Eloquent, it’s

Laravel Eloquent ORM: Select from two tables without using find() possible?

Basically I would like to run a very simple database select query accross two tables in mysql considering a one-to-many-relationship. categories: This table contains all product-categories. The primary key is “id”. It has one column “url” which holds the string how to access this category, e.g. “greeting-cards”. products: This table contains all products whereas each product has one category id.

Get all products where average rating is equal to 5 in laravel

I have 2 Tables Products and Reviews Products hasMany reviews Reviews belongsTo Product I want to get only those product which have avg(‘ratings’) is equal to 5 from controller, I get products = Product::with(‘category’)->with(‘reviews’)->get(); now want show in blade in one part of Top Rated Products $product->reviews()->avg(‘ratings’) I used this to get average ratings for a single Product. Now I

Advertisement