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
133
votes
14 answers

Laravel Eloquent - distinct() and count() not working properly together

So I'm trying to get the number of distinct pids on a query, but the returned value is wrong. This is what I try to do: $ad->getcodes()->groupby('pid')->distinct()->count() what returns the value "2", while the value it should return, should be…
Inigo EC
  • 2,178
  • 3
  • 22
  • 31
133
votes
7 answers

Laravel Eloquent - Get one Row

This might be a simple question, but I cannot figure this out. I am trying to get a user by email using: $user = User::whereEmail($email)->get(); But this is returning an array (of dimension 1) of $users. So If I want to get the name, I have to do…
Kousha
  • 32,871
  • 51
  • 172
  • 296
133
votes
4 answers

Managing relationships in Laravel, adhering to the repository pattern

While creating an app in Laravel 4 after reading T. Otwell's book on good design patterns in Laravel I found myself creating repositories for every table on the application. I ended up with the following table structure: Students: id, name Courses:…
ehp
  • 1,689
  • 3
  • 14
  • 20
131
votes
10 answers

Laravel Eloquent limit and offset

This is mine $art = Article::where('id',$article)->firstOrFail(); $products = $art->products; I just wanna take a limit 'product' This is wrong way $products = $art->products->offset($offset*$limit)->take($limit)->get(); Please give me…
Sang Trần
  • 2,147
  • 4
  • 13
  • 20
126
votes
8 answers

laravel collection to array

I have two models, Post and Comment; many comments belong to a single post. I'm trying to access all comments associated with a post as an array. I have the following, which gives a collection. $comments_collection = $post->comments()->get() How…
datavoredan
  • 3,536
  • 9
  • 32
  • 48
121
votes
6 answers

Laravel Eloquent update just if changes have been made

Is there any way to update a record in Laravel using eloquent models just if a change has been made to that record? I don't want any user requesting the database for no good reason over and over, just hitting the button to save changes. I have a…
user2755140
  • 1,917
  • 4
  • 13
  • 16
120
votes
7 answers

Preventing Laravel adding multiple records to a pivot table

I have a many to many relationship set up and working, to add an item to the cart I use: $cart->items()->attach($item); Which adds an item to the pivot table (as it should), but if the user clicks on the link again to add an item they have already…
Al_
  • 2,479
  • 7
  • 29
  • 43
119
votes
12 answers

Clone an Eloquent object including all relationships?

Is there any way to easily clone an Eloquent object, including all of its relationships? For example, if I had these tables: users ( id, name, email ) roles ( id, name ) user_roles ( user_id, role_id ) In addition to creating a new row in the users…
andrewtweber
  • 24,520
  • 22
  • 88
  • 110
119
votes
2 answers

How to get all rows (soft deleted too) from a table in Laravel?

To get all rows from a table, I have to use Model::all() but (from good reason) this doesn't gives me back the soft deleted rows. Is there a way I can accomplish this with Eloquent?
totymedli
  • 29,531
  • 22
  • 131
  • 165
118
votes
5 answers

eloquent laravel: How to get a row count from a ->get()

I'm having a lot of trouble figuring out how to use this collection to count rows. $wordlist = \DB::table('wordlist')->where('id', '<=', $correctedComparisons) ->get(); I have tried adding->count() but didn't work. I have tried…
JP Foster
  • 1,725
  • 4
  • 17
  • 23
114
votes
4 answers

Laravel Eloquent where field is X or null

I have a table like this: table - field1: tinyint - field2: varchar (nullable) - datefield: timestamp (nullable) Now I want to get all entries where field1 is 1, field2 is null and where datefield is smaller than X or null. I already tried…
tinyoverflow
  • 1,933
  • 3
  • 13
  • 29
113
votes
8 answers

Laravel Eloquent vs DB facade: When to use which?

I did some performance tests between Laravel's DB facade query builder and Laravel's Eloquent ORM. The DB facade was much faster than Eloquent for many SQL statements (SELECT, UPDATE, DELETE, INSERT). So why would someone use the slower Laravel…
Panagiotis Koursaris
  • 3,794
  • 4
  • 23
  • 46
113
votes
9 answers

Laravel, sync() - how to sync an array and also pass additional pivot fields?

Official Laravel documentation has this on sync() function: $user->roles()->sync( array( 1, 2, 3 ) ); You may also associate other pivot table values with the given IDs: $user->roles()->sync( array( 1 => array( 'expires' => true ) ) ); In the…
Томица Кораћ
  • 2,542
  • 7
  • 35
  • 57
113
votes
6 answers

Laravel save / update many to many relationship

Can anyone help me on how to save many to many relationship? I have tasks, user can have many tasks and task can have many users (many to many), What I want to achieve is that in update form admin can assign multiple users to specific task. This is…
SuperManSL
  • 1,306
  • 2
  • 12
  • 17
111
votes
6 answers

Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0

I am trying to use email as my table's primary key, so my eloquent code is-
Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161