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
825
votes
40 answers

How do I get the query builder to output its raw SQL query as a string?

Given the following code: DB::table('users')->get(); I want to get the raw SQL query string that the database query builder above will generate. In this example, it would be SELECT * FROM users. How do I do this?
meiryo
  • 11,157
  • 14
  • 47
  • 52
584
votes
26 answers

How to Create Multiple Where Clause Query Using Laravel Eloquent?

I'm using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it's not elegant. Example: $results = User::where('this', '=', 1) ->where('that', '=', 1) ->where('this_too',…
veksen
  • 6,863
  • 5
  • 20
  • 33
407
votes
32 answers

Get the Last Inserted Id Using Laravel Eloquent

I'm currently using the below code to insert data in a table: nombre = $post['name']; $data->direccion = $post['address']; …
SoldierCorp
  • 7,610
  • 16
  • 60
  • 100
401
votes
33 answers

Laravel Checking If a Record Exists

I am new to Laravel. How do I find if a record exists? $user = User::where('email', '=', Input::get('email')); What can I do here to see if $user has a record?
Ben
  • 5,627
  • 9
  • 35
  • 49
400
votes
15 answers

Laravel Eloquent Query: Using WHERE with OR AND OR?

How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1) For more complicated queries am I supposed to use raw SQL?
Farzher
  • 13,934
  • 21
  • 69
  • 100
375
votes
17 answers

Laravel - Eloquent or Fluent random row

How can I select a random row using Eloquent or Fluent in Laravel framework? I know that by using SQL, you can do order by RAND(). However, I would like to get the random row without doing a count on the number of records prior to the initial…
DigitalWM
  • 4,406
  • 3
  • 18
  • 15
372
votes
2 answers

Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean?

I've found the concept and meaning behind these methods to be a little confusing, is it possible for somebody to explain to me what the difference between has and with is, in the context of an example (if possible)?
user4898812
351
votes
13 answers

Eloquent Collection: Counting and Detect Empty

This may be a trivial question but I am wondering if Laravel recommends a certain way to check whether an Eloquent collection returned from $result = Model::where(...)->get() is empty, as well as counting the number of elements. We are currently…
bitinn
  • 9,188
  • 10
  • 38
  • 64
333
votes
20 answers

Get Specific Columns Using “With()” Function in Laravel Eloquent

I have two tables, User and Post. One User can have many posts and one post belongs to only one user. In my User model I have a hasMany relation... public function post(){ return $this->hasmany('post'); } And in my post model I have a…
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
318
votes
12 answers

Laravel Migration Change to Make a Column Nullable

I created a migration with unsigned user_id. How can I edit user_id in a new migration to also make it nullable()? Schema::create('throttle', function(Blueprint $table) { $table->increments('id'); // this needs to also be nullable, how…
user391986
  • 29,536
  • 39
  • 126
  • 205
318
votes
5 answers

How to sort a Laravel query builder result by multiple columns?

I want to sort multiple columns in Laravel 4 by using the method orderBy() in Laravel Eloquent. The query will be generated using Eloquent like this: SELECT * FROM mytable ORDER BY coloumn1 DESC, coloumn2 ASC How can I do this?
Sophy
  • 8,845
  • 6
  • 36
  • 30
314
votes
11 answers

How do you check "if not null" with Eloquent?

How do you check if a field is not null with Eloquent? I tried Model::where('sent_at', 'IS NOT', DB::raw('null'))->... but it gives IS NOT as a binding instead of a comparison. This is what DB::getQueryLog() says about it: 'query' => string…
Marwelln
  • 28,492
  • 21
  • 93
  • 117
289
votes
13 answers

Laravel Eloquent: Ordering results of all()

I'm stuck on a simple task. I just need to order results coming from this call $results = Project::all(); Where Project is a model. I've tried this $results = Project::all()->orderBy("name"); But it didn't work. Which is the better way to obtain…
MatterGoal
  • 16,038
  • 19
  • 109
  • 186
282
votes
10 answers

Add a custom attribute to a Laravel / Eloquent model on load?

I'd like to be able to add a custom attribute/property to an Laravel/Eloquent model when it is loaded, similar to how that might be achieved with RedBean's $model->open() method. For instance, at the moment, in my controller I have: public function…
coatesap
  • 10,707
  • 5
  • 25
  • 33
277
votes
11 answers

Disable Laravel's Eloquent timestamps

I'm in the process of converting one of our web applications from CodeIgniter to Laravel. However at this moment we don't want to add the updated_at / created_at fields to all of our tables as we have a logging class that does all this in more depth…
projectxmatt
  • 3,091
  • 2
  • 15
  • 16
1
2 3
99 100