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
91
votes
3 answers

Laravel 5 Eloquent where and or in Clauses

i try to get results from table with multiple where and/or clauses. My SQL statement is: SELECT * FROM tbl WHERE m__Id = 46 AND t_Id = 2 AND (Cab = 2 OR Cab = 4) How i can get this with Laravel Eloquent? My Code in Laravel is: $BType =…
Sinisa P.
  • 1,112
  • 2
  • 9
  • 14
90
votes
4 answers

Filtering Eloquent collection data with $collection->filter()

I'm trying to filter the following collection using the collection filter() method: $collection = Word::all(); where the JSON output looks like this: [ { "id": "1", "word": "dog", "phonetic": "dog", "mean": "pies", "assoc": "some example…
Tenzoru
  • 953
  • 1
  • 8
  • 9
90
votes
10 answers

Update without touching timestamps (Laravel)

Is it possible to update a user without touching the timestamps? I don't want to disable the timestamps completly.. grtz
Jannick Vandaele
  • 963
  • 1
  • 7
  • 9
87
votes
4 answers

Laravel 5 Querying with relations causes "Call to a member function addEagerConstraints() on null" error

I have been trying to create a simple user management system but keep on hitting road blocks when it comes to querying relations. For example I have users and roles and whenever I try to make a query for all users and their roles I get an error. …
Tomkarho
  • 1,789
  • 3
  • 19
  • 25
87
votes
5 answers

Eloquent ->first() if ->exists()

I want to get the first row in table where condition matches: User::where('mobile', Input::get('mobile'))->first() It works well, but if the condition doesn't match, it throws an Exception: ErrorException Trying to get property of…
Positivity
  • 5,406
  • 6
  • 41
  • 61
87
votes
7 answers

Laravel OrderBy relationship count

I'm trying to get the most popular hackathons which requires ordering by the respective hackathon's partipants->count(). Sorry if that's a little difficult to understand. I have a database with the following format: hackathons id name …
Joe Torraca
  • 1,949
  • 5
  • 31
  • 50
85
votes
5 answers

Laravel 5.2 - pluck() method returns array

I'm trying to upgrade my project L5.1 -> L5.2. In upgrade guide there's one thing which isn't clear for me: The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature …
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
84
votes
3 answers

Get previous attribute value in Eloquent model event

Is there a way to see the old/previous value of a model's attribute in its saving or updating event? eg. Is something like the following possible: User::updating(function($user) { if ($user->username != $user->old->username) doSomething(); });
coatesap
  • 10,707
  • 5
  • 25
  • 33
81
votes
5 answers

Laravel Eloquent: eager loading of multiple nested relationships

What laravel says: $books = App\Book::with('author.contacts')->get(); What I need is something like this $books = App\Book::with('author[contacts,publishers]')->get(); where we eager load multiple relationships within a relationship. Is this…
DrivingInsanee
  • 1,631
  • 2
  • 13
  • 22
81
votes
4 answers

Laravel firstOrNew how to check if it's first or new?

I'm using Laravel's function firstOrNew() to create a new user or find and update an existing one. How can I know, after the object is created, if it existed before or if it's a new object? The idea is something like this: $user =…
sigmaxf
  • 7,998
  • 15
  • 65
  • 125
81
votes
7 answers

Eloquent eager load Order by

I have problem with eloquent query. I am using eager loading (one to one Relationship) to get 'student' With the 'exam', Using the code below. Student::with('exam')->orderBy('exam.result', 'DESC')->get() And i want to order received rows by the…
Andrius
  • 931
  • 1
  • 6
  • 7
80
votes
2 answers

How to select count with Laravel's fluent query builder?

Here is my query using fluent query builder. $query = DB::table('category_issue') ->select('issues.*') ->where('category_id', '=', 1) ->join('issues', 'category_issue.issue_id', '=', 'issues.id') …
Alex Naspo
  • 2,052
  • 1
  • 20
  • 37
79
votes
7 answers

laravel Eloquent ORM delete() method

Hi I am studying laravel. I use Eloquent ORM delete method but I get a different result.Not true or false but null. I set an resource route and there is a destroy method in UsersController. public function destroy($id){ …
Evol Rof
  • 2,528
  • 2
  • 22
  • 37
78
votes
5 answers

Find max value of a column in laravel

The problem started because I have a table (Clientes), in which the primary key is not auto-incremental. I want to select the max value stored in a column database. Like this select, but with eloquent ORM (Laravel): SELECT MAX(Id) FROM…
Guido Caffa
  • 1,201
  • 1
  • 12
  • 22
78
votes
19 answers

Eloquent error: A facade root has not been set

I have been using Eloquent as a standalone package in Slim Framework 2 successfully. But now that I want to make use of Illuminate\Support\Facades\DB since I need to show some statistics by getting the info from 2 tables and using a Left Join and a…
Pathros
  • 10,042
  • 20
  • 90
  • 156