Questions tagged [eloquent]

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

The Eloquent included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Resources

26966 questions
33
votes
5 answers

How To Cast Eloquent Pivot Parameters?

I have the following Eloquent Models with relationships: class Lead extends Model { public function contacts() { return $this->belongsToMany('App\Contact') ->withPivot('is_primary'); } } class Contact…
Josh
  • 8,079
  • 3
  • 24
  • 49
33
votes
2 answers

Laravel adding data to pivot table while inserting new record

I have three tables, roles(id, name); users(id, email, password); user_role(id, user_id, role_id); In this scenario, I have users table is associated to roles table with many to many relation. I have two eloquent model as Role +users(){ …
Dipendra Gurung
  • 5,720
  • 11
  • 39
  • 62
33
votes
7 answers

How to order by pivot table data in Laravel's Eloquent ORM

In my Database, I have: tops Table posts Table tops_has_posts Table. When I retrieve a top on my tops table I also retrieve the posts in relation with the top. But what if I want to retrieve these posts in a certain order ? So I add a range field…
KeizerBridge
  • 2,707
  • 7
  • 24
  • 37
33
votes
7 answers

Laravel belongsTo not working

I have 2 models in my app, 'User' & 'MedicineType' (each User belongs to one MedicineType). I made the one-to-many relation between two model using belongsTo() and hasMany(). hasMany() relation works perfectly but belongTo() doesn't work. Does…
Yashari
  • 491
  • 1
  • 8
  • 14
33
votes
3 answers

Sort Eloquent Collection by created_at

I have a table named 'posts' with the columns: 'post_id int primary increments', 'poster_id int' and 'status text' as well as an array named friends with the columns: 'user_id int primary' and 'friend_ids text'. I need to grab all the IDs in the…
Liam Potter
  • 1,732
  • 8
  • 24
  • 47
33
votes
3 answers

Query relationship Eloquent

I have News model, and News has many comments, so I did this in News model: public function comments(){ $this->hasMany('Comment', 'news_id'); } But I also have field trashed in comments table, and I only want to select comments that are not…
Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65
33
votes
8 answers

truncate all tables in laravel using eloquent

Is there a way I could truncate all the tables in a db using eloquent or fluent in laravel 4? I do not want to specify table names, I just want to truncate all the tables. In other words empty all the tables.
Mounir
  • 393
  • 1
  • 7
  • 11
33
votes
2 answers

Laravel join queries AS

Any way of defining an AS for a query?? I have tried the following: $data = News::order_by('news.id', 'desc') ->join('categories', 'news.category_id', '=', 'categories.id') ->left_join('users', 'news.user_id', '=', 'users.id') //…
Alex
  • 7,538
  • 23
  • 84
  • 152
33
votes
5 answers

How to sort by a field of the pivot table of a many-to-many relationship in Eloquent ORM

I have been using Eloquent ORM for some time now and I know it quite well, but I can't do the following, while it's very easy to do in Fluent. I have users with a many-to-many songs, intermediate table being song_user (like it should be). I'd like…
duality_
  • 17,738
  • 23
  • 77
  • 95
32
votes
4 answers

Laravel get a collection of relationship items

I'm stuck on this what seems like a simple task. I have a User that has many Shops that have many Products.. I'm trying to get all the Products for a certain User. This is working, and is returning the Shops with their…
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
32
votes
4 answers

How to delete multiple records using Laravel Eloquent

Now this, from what I can see, should have been simple. I want to be able to delete multiple records from the database. I have the id's of all the records I wish to delete. I call the resource.destroy route using comma separated list of ids (id is…
Code Poet
  • 11,227
  • 19
  • 64
  • 97
32
votes
5 answers

Laravel - Where to store statuses (flags)? Model, Class or config folder?

I need to extensively use statuses in mt project. I need them for my users (active, suspended, etc), an entity (active, pending_activation, inactive) and for my subscriptions(active, on_grace_period, not_subscribed, never_subscribed). So far I…
Cristian
  • 2,390
  • 6
  • 27
  • 40
32
votes
1 answer

Get ids array from related laravel model which is having belongsToMany relationship

I have a model Role which belongs to many Users. Class Role { public $fillable = ["name"]; public function users() { return $this->belongsToMany('App/Models/User')->select(['user_id']); } } When I retrieve users…
sp11
  • 323
  • 1
  • 3
  • 6
32
votes
4 answers

Laravel eloquent search on fields of related model

I have an eloquent models as, User : users(id, username, password, email, status) Profile : profiles(id, user_id, first_name, last_name, gender, dob) In the controller logic, I am eagerly loading the Profile model. I can do this, $user =…
Dipendra Gurung
  • 5,720
  • 11
  • 39
  • 62
32
votes
4 answers

Has one through Laravel Eloquent

I have three tables garages, cars, securities. The securities are the ones that is keeping one car safe, you can have more than one security, but a single security can keep only one car safe. The garage is where the car is and the securities are as…
yayuj
  • 2,194
  • 3
  • 17
  • 29