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

Laravel Eloquent Lazy Eager Load Count

I'm ideally looking for a function like load('relationship') but which loads a count in the same way withCount('relationship') works for eager loading. I'm thinking it is going to be called loadCount('relationship')
Edward Louth
  • 1,010
  • 2
  • 9
  • 19
55
votes
14 answers

PHPStorm is not recognizing methods of my Model class in Laravel 5.0

failed insert data into database, and all query class and Model class's method not found show in IDE (phpStrom) how can I solve it? here is my extended class (Post.php) here show error in latest and where method:
Osman Goni Nahid
  • 1,193
  • 2
  • 15
  • 24
55
votes
9 answers

Laravel: Returning the namespaced owner of a polymorphic relation

I can find a number of discussions regarding this but no clear solution. Here are two links, although I will cover everything in my own question here. Github Issues Laravel.io discussion Simple Explanation This is a simple explanation of my problem…
John Mellor
  • 2,351
  • 8
  • 45
  • 79
54
votes
10 answers

How is a pivot table created by Laravel?

In Laravel 4, when working with many-to-many relationships as described in the 4.2 docs, how can I actually get Laravel to create the pivot table for me? Do I need to add something in my migrations for the two models that are involved? Do I need to…
Ben
  • 15,938
  • 19
  • 92
  • 138
53
votes
3 answers

How to use break or continue with Laravel Eloquent Collection's each method?

How to use break or continue with Laravel Eloquent Collection's each method. My code is this: $objectives->each(function($objective) { Collection::make($objective)->each(function($action) { …
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38
53
votes
4 answers

Saving related records in laravel

I have users, and users belong to a dealership. Upon user registration, I'm trying to save a new user, and a new dealership. User database has a dealership_id column, which I want to be populated with the ID of the newly created dealership. This…
BeardedInBinary
  • 705
  • 1
  • 8
  • 15
53
votes
2 answers

Laravel eloquent ORM group where

How do I convert the following query to Laravel 4 eloquent ORM? select * from table where ((starttime <= ? and endtime >= ?) or (starttime <= ? and endtime >= ?) or (starttime >= ? and endtime <= ?))
Bishwarup Das
  • 681
  • 1
  • 12
  • 21
53
votes
4 answers

Laravel Eloquent with and find

Why is this not working? Article::with('category')->find($ids) I got a Array to String Conversion Exception. But if i split the query into 2 parts like this: $articles = Article::with('category') and $articles = $articles->find($ids) I didn't…
Marco
  • 1,579
  • 1
  • 19
  • 34
52
votes
3 answers

Does Laravel's "soft_delete" need index on MySQL?

If i'm using soft delete in laravel 4.2 (database is mysql), every eloquent query has WHERE deleted_at IS NULL. There are no indexes on deleted_at. Will it be slow on big tables? (or maybe IS NULL is optimized without needing an index) Should I…
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
52
votes
9 answers

How can I query raw via Eloquent?

I am trying to do a query in my Laravel app and I want to use a normal structure for my query. This class either does use Eloquent so I need to find something to do a query totally raw. Might be something like Model::query($query);. Only that…
Chilion
  • 4,380
  • 4
  • 33
  • 48
51
votes
1 answer

In Laravel, is there a way to find out whether `firstOrCreate` created or if it found the row?

In Laravel, is there a way to differentiate between firstOrCreate creating the row and if finding out the row exists from before? Or will I have to manually do a get() first, and then create the row?
Pat
  • 1,193
  • 1
  • 11
  • 36
51
votes
1 answer

naming tables in many to many relationships laravel

I concerned about auto naming tables in many-to-many Laravel relationship. for example: Schema::create('feature_product', function (Blueprint $table) {} when change the table name to: Schema::create('product_feature', function (Blueprint $table)…
Pedram marandi
  • 1,474
  • 1
  • 19
  • 35
50
votes
3 answers

Laravel Eloquent - How to query NOT LIKE?

How might I find something that is not like a certain string. SELECT * FROM users WHERE username NOT LIKE '%ray%';
Jeremy
  • 3,620
  • 9
  • 43
  • 75
50
votes
4 answers

Laravel Eloquent Relations: ->latest()

What is the function of latest() in laravel? Example: public function activity() { return $this->hasMany('App\Activity') ->with(['user', 'subject']) ->latest(); } From Build an activity feed in Laravel on line 44. I've been…
user3253002
  • 1,521
  • 3
  • 21
  • 43
50
votes
6 answers

Synchronizing a one-to-many relationship in Laravel

If I have a many-to-many relationship it's super easy to update the relationship with its sync method. But what would I use to synchronize a one-to-many relationship? table posts: id, name table links: id, name, post_id Here, each Post can have…
user2834172
  • 861
  • 1
  • 9
  • 17