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

How to filter a pivot table using Eloquent?

I'm using a pivot table on the project I'm working with to get works of users. E.g: User::find(1)->works gives me the works of user with ID of 1. The thing is that I want to filter this results with extra Pivot data. Something…
Arda
  • 6,756
  • 3
  • 47
  • 67
39
votes
3 answers

How to pass parameter to Laravel DB::transaction()

From the laravel documentation: Database Transaction. It says that: DB::transaction(function() { DB::table('users')->update(array('votes' => 1)); DB::table('posts')->delete(); }); Here, 1 is explicitly entered to update the users... I…
Melvin
  • 5,798
  • 8
  • 46
  • 55
38
votes
2 answers

Migration Foreign Key Vs Eloquent Relationships in Laravel

In Laravel 5.1 I can see that table column relationships can be set-up in 2 ways: 1) Defining Foreign Keys in the Migration table. 2) Defining the Eloquent relationships in the Models. I have read the documentations and I am still confused on the…
Neel
  • 9,352
  • 23
  • 87
  • 128
38
votes
5 answers

Laravel Eloquent how to get the second or third record?

I have an Order and a Product models. "Order" HasMany Product (and Product belongsTo Order)... Let's say I want to display the 3 products of my order, how to do that ? I know the first could be retrieved like $order->products->first()... but how…
nadir
  • 1,223
  • 4
  • 12
  • 21
38
votes
5 answers

Laravel - Eager Loading Polymorphic Relation's Related Models

I can eager load polymorphic relations/models without any n+1 issues. However, if I try to access a model related to the polymorphic model, the n+1 problem appears and I can't seem to find a fix. Here is the exact setup to see it locally: 1) DB…
Wonka
  • 8,244
  • 21
  • 73
  • 121
38
votes
1 answer

Eloquent sync and created_at/updated_at

I have the following code: $entry->save(); $categories = []; $categories[Input::get('main_category')] = ['main' => 1]; for ($i=1; $i<=4 ; ++$i) { $input = Input::get('category_'.$i); if ($input != '') { $categories[$input] =…
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
38
votes
2 answers

Laravel : Passing extra parameter on Collection filtering

the idea it's quite simple, however I have not yet been able to materialize it. Here's the code (I've changed the name of the variables to describe their use) $games = Game::all(); $games_already_added = $member->games()->lists('id'); …
Joel Hernandez
  • 1,817
  • 4
  • 18
  • 27
38
votes
2 answers

Eloquent where condition based on a "belongs to" relationship

Let's say I have the following model: class Movie extends Eloquent { public function director() { return $this->belongsTo('Director'); } } Now I'd like fetch movies using a where condition that's based on a column from the…
Lior
  • 5,454
  • 8
  • 30
  • 38
38
votes
4 answers

Get array of Eloquent model's relations

I'm trying to get an array of all of my model's associations. I have the following model: class Article extends Eloquent { protected $guarded = array(); public static $rules = array(); public function author() { return…
tprsn
  • 757
  • 2
  • 7
  • 18
38
votes
4 answers

Laravel 4 - Get Array of Attributes from Collection

I have a collection of objects. Let's say the objects are tags: $tags = Tag::all(); I want to retrieve a certain attribute for each tag, say its name. Of course I can do foreach ($tags as $tag) { $tag_names[] = $tag->name; } But is there a…
severin
  • 2,106
  • 1
  • 17
  • 25
38
votes
2 answers

Laravel 4 Eloquent ORM select where - array as parameter

Is solution for this in Eloquent ORM? I have array with parents idetifiers: Array ( [0] => 87, [1] => 65, ... ) And i want select table PRODUCTS where parent_id column = any id in array
Lajdák Marek
  • 2,969
  • 8
  • 29
  • 58
37
votes
6 answers

Laravel using where clause on a withCount method

I am trying to do a where clause on withCount method of laravel's eloquent query builder using this piece of code. $posts = Post::withCount('upvotes')->where('upvotes_count', '>', 5)->get(); and this code is giving me this error. SQLSTATE[42S22]:…
slapbot
  • 627
  • 1
  • 6
  • 17
37
votes
8 answers

Eloquent ORM Code Hinting in PhpStorm

So I'm just starting off with Laravel (using v5) and Eloquent. I'm working on getting some basic APIs up and running and noticing that a lot of working methods don't show up in PhpStorm's code hinting So I have this model: namespace…
Josh
  • 8,079
  • 3
  • 24
  • 49
37
votes
7 answers

Why soft deleted entities appear in query results?

I am trying to implement soft deleting concept. Here is my object: class Post extends Eloquent { /** * The database table used by the model. * * @var string */ protected $table = 'posts'; protected $softDelete =…
Sergey Sob
  • 815
  • 1
  • 12
  • 27
36
votes
3 answers

How to increment and update column in one eloquent query

Is it possible to update a timestamp (besides updated_at) and increment a column in one query? I obviously can ->increment('count') and separately ->update(['last_count_increased_at' => Carbon::now()]) but is there an easy way to do both…
Alex Harris
  • 6,172
  • 2
  • 32
  • 57