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
111
votes
9 answers

Eloquent ORM laravel 5 Get Array of ids

I'm using Eloquent ORM laravel 5.1, and I want to return an array of ids greater than 0, my model is called test. I have tried : $test=test::select('id')->where('id' ,'>' ,0)->get()->toarray(); It returns : Array ( [0] => Array ( [id] => 1 ) [1] =>…
paranoid
  • 6,799
  • 19
  • 49
  • 86
108
votes
6 answers

Change default primary key in Eloquent

Can I change Eloquent model primary key. I want to set primary key for example admin_id instead of 'id'? I know I can change table name for model like protected $table = "admin"; Is there something similar for primary key?
Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65
107
votes
16 answers

Laravel Eloquent: How to get only certain columns from joined tables

I have got 2 joined tables in Eloquent namely themes and users. theme model: public function user() { return $this->belongs_to('User'); } user model: public function themes() { return $this->has_many('Theme'); } My Eloquent api call looks as…
ralu
  • 1,191
  • 2
  • 7
  • 7
106
votes
11 answers

Why I'm getting 'Non-static method should not be called statically' when invoking a method in a Eloquent model?

Im trying to load my model in my controller and tried this: return Post::getAll(); got the error Non-static method Post::getAll() should not be called statically, assuming $this from incompatible context The function in the model looks like…
Sam Pettersson
  • 3,049
  • 6
  • 23
  • 37
101
votes
5 answers

Laravel - find by custom column or fail

There's findOrFail() method which throws 404 if nothing was found, e.g.: User::findOrFail(1); How can I find an entity by custom column or fail, something like this: Page::findBySlugOrFail('about');
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
99
votes
5 answers

Laravel eloquent update record without loading from database

I'm quite new to laravel and I'm trying to update a record from form's input. However I see that to update the record, first you need to fetch the record from database. Isn't is possible to something like to update a record (primary key is…
Dester Dezzods
  • 1,417
  • 4
  • 17
  • 33
99
votes
1 answer

Timestamps are not updating while attaching data in pivot table

I'm creating a row in pivot table using the following attach statement. $music = Music::find(1); $music->users()->attach(1); This inserting a row in pivot table, but it is not updating the timestamp. The timestamp remains as 0000-00-00 00:00:00. Is…
Sriraman
  • 7,658
  • 4
  • 40
  • 60
99
votes
20 answers

Laravel update model with unique validation rule for attribute

I have a Laravel User model which has a unique validation rule on username and email. In my Repository, when I update the model, I revalidate the fields, so as to not have a problem with required rule validation: public function update($id, $data)…
Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59
97
votes
5 answers

Using Eloquent ORM in Laravel to perform search of database using LIKE

I want to use Eloquent's active record building to build a search query, but it is going to be a LIKE search. I have found the User::find($term) or User::find(1), but this is not generating a like statement. I'm not looking for a direct answer, but…
Jonathan
  • 1,135
  • 1
  • 10
  • 15
96
votes
5 answers

First Or Create

I know using: User::firstOrCreate(array('name' => $input['name'], 'email' => $input['email'], 'password' => $input['password'])); Checks whether the user exists first, if not it creates it, but how does it check? Does it check on all the params…
panthro
  • 22,779
  • 66
  • 183
  • 324
95
votes
8 answers

How to access model hasMany Relation with where condition?

I created a model Game using a condition / constraint for a relation as follows: class Game extends Eloquent { // many more stuff here // relation without any constraints ...works fine public function videos() { return…
Remluben
  • 1,613
  • 2
  • 16
  • 21
94
votes
3 answers

Laravel Eloquent - Attach vs Sync

What is the difference between attach() and sync() in Laravel 4's Eloquent ORM? I've tried to look around but couldn't find anything!
Kousha
  • 32,871
  • 51
  • 172
  • 296
94
votes
11 answers

How to exclude certains columns while using eloquent

When I'm using eloquent, I can use the "where" method then the method 'get' to fill an object containing what I've selected in my database. I mean: $users = User::where('gender', 'M')->where('is_active', 1)->get(['pseudo', 'email', 'age',…
Brazeredge
  • 1,061
  • 1
  • 7
  • 12
94
votes
9 answers

How to set every row to the same value with Laravel's Eloquent/Fluent?

I need to update all of the rows in a database so that a particular field in all of them is equal to a single value. Here's an example. Let's say my database table is like…
Pete
  • 7,289
  • 10
  • 39
  • 63
92
votes
13 answers

Laravel: How to get last N entries from DB

I have table of dogs in my DB and I want to retrieve N latest added dogs. Only way that I found is something like this: Dogs:all()->where(time, <=, another_time); Is there another way how to do it? For example something like this…
Jakub Kohout
  • 1,854
  • 3
  • 21
  • 36