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
50
votes
2 answers

Laravel Eloquent Filter By Column of Relationship

Using the Eloquent ORM I have my models set up like so: Post belongsToMany Category Post.php public function categories() { return $this->belongsToMany('Category', 'posts_categories'); } I want to filter posts by a column of the categories…
iamjonesy
  • 24,732
  • 40
  • 139
  • 206
49
votes
16 answers

How can I solve incompatible with sql_mode=only_full_group_by in laravel eloquent?

My laravel eloquent is like this : $products = Product::where('status', 1) ->where('stock', '>', 0) ->where('category_id', '=', $category_id) ->groupBy('store_id') ->orderBy('updated_at', 'desc') …
samuel toh
  • 6,836
  • 21
  • 71
  • 108
49
votes
3 answers

Laravel Soft Delete restore() Error

The following soft delete code works fine for me: $post = Post::find($post_id); $post->delete(); The deleted_at field is updated. But this gives me an error: $post = Post::find($post_id); $post->restore(); Here's the error: exception…
sterfry68
  • 1,063
  • 2
  • 14
  • 30
49
votes
6 answers

Laravel Eloquent LEFT JOIN WHERE NULL

I'm trying to use Eloquent to perform the following query during a database seed: SELECT * FROM customers LEFT JOIN orders ON customers.id = orders.customer_id WHERE orders.customer_id IS NULL And here is my implementation in…
eComEvo
  • 11,669
  • 26
  • 89
  • 145
49
votes
3 answers

What is the difference between destroy() and delete() methods in Laravel?

I'm having a minor issue with Laravel 4. I'd like to use the delete() method on a record but for some reason it doesn't actually delete the record. destroy() does, though, so my code is good. Also, if I pass Teetime::where('date', '=',…
user347284
49
votes
4 answers

Retrieving relationships of relationships using Eloquent in Laravel

I have a database with the following tables and relationships: Advert 1-1 Car m-1 Model m-1 Brand If I want to retrieve an Advert, I can simply use: Advert::find(1); If I want the details of the car, I could…
Ben Thompson
  • 4,743
  • 7
  • 35
  • 52
48
votes
11 answers

How to sort NULL values last using Eloquent in Laravel

I've got a many to many relationship between my employees and groups table. I've created the pivot table, and all is working correctly with that. However, I've got a sortOrder column on my employees table that I use to determine the order in which…
eagle0042
  • 507
  • 2
  • 7
  • 12
47
votes
3 answers

Create Models from database in Laravel

Is there some way to generate models from database in Laravel? The generators package only create an empty model.
Santi Barbat
  • 2,097
  • 2
  • 21
  • 26
47
votes
3 answers

Laravel Eloquent Compare Column Values

Eloquent's where() seems not working when comparing two column values. How to fix it? Sample code: ->where('table_1.name', '=', 'table_2.name') But works on: ->where('table_1.name', '=', 'john')
Jake Opena
  • 1,475
  • 1
  • 11
  • 18
47
votes
4 answers

Laravel $q->where() between dates

I am trying to get my cron to only get Projects that are due to recur/renew in the next 7 days to send out reminder emails. I've just found out my logic doesn't quite work. I currently have the query: $projects = Project::where(function($q){ …
Jono20201
  • 3,215
  • 3
  • 20
  • 33
46
votes
4 answers

Laravel eloquent where date is equal or greater than DateTime

I'm trying to fetch relational data from a model where the date column is higher or equal to the current time. The date column is formated as this: Y-m-d H:i:s What I'm trying to do is to grab all rows where the Y-m-d H:i:s is in the…
Kaizokupuffball
  • 2,703
  • 8
  • 38
  • 58
46
votes
7 answers

PHP Lumen Call to a member function connection() on null

I'm receiving this error when trying to use an Eloquent Model in Lumen. Call to a member function connection() on null Controller func: /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ …
bi4nchi
  • 519
  • 1
  • 4
  • 7
46
votes
6 answers

Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

Using Laravel, I have the following code $review = Review::find(1); $review->delete(); Review has a many to many relationship defined with a Product entity. When I delete a review, I'd expect it to be detached from the associated products in the…
Grant J
  • 1,056
  • 3
  • 10
  • 18
46
votes
4 answers

What is the syntax for sorting an Eloquent collection by multiple columns?

I know that when using the query builder, it is possible to sort by multiple columns using ...orderBy('column1')->orderBy('column2') but now I am dealing with a collection object. Collections have the sortBy method, but I have not been able to…
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
46
votes
13 answers

How to change default format at created_at and updated_at value laravel

I am new in Laravel. I am creating a application with laravel. When i creating a post then the values of "created_at" and 'updated_at" are look like this: 2014-06-26 04:07:31 2014-06-26 04:07:31 But i want this values without time look like this…
saiful408
  • 501
  • 1
  • 4
  • 9