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
77
votes
5 answers

How to reload/refresh model from database in Laravel?

In some of my tests, I have a user model I have created and I run some methods that need to save certain attributes. In rails, I would typically call something like user.reload which would repopulate the attributes from the database. Is there a way…
matthew
  • 817
  • 1
  • 6
  • 7
77
votes
9 answers

How to manually create a new empty Eloquent Collection in Laravel 4

How do we create a new Eloquent Collection in Laravel 4, without using Query Builder? There is a newCollection() method which can be overridden by that doesn't really do job because that is only being used when we are querying a set result. I was…
JofryHS
  • 5,804
  • 2
  • 32
  • 39
77
votes
8 answers

Laravel Eloquent inner join with multiple conditions

I have a question about inner joins with multiple on values. I did build my code like this in laravel. public function scopeShops($query) { return $query->join('kg_shops', function($join) { $join->on('kg_shops.id', '=',…
Wouter Neuteboom
  • 796
  • 1
  • 8
  • 22
76
votes
20 answers

Laravel how to get query with bindings?

I have some query that I need to pass to another query using query builder $query = DB::table('table')->whereIn('some_field', [1,2,30])->toSql(); Model::join(DB::raw("({$query}) as table"), function($join) { $join->on('model.id', '=',…
Mateusz Nowak
  • 4,021
  • 2
  • 25
  • 37
75
votes
2 answers

hasMany vs belongsToMany in laravel 5.x

I'm curious why the Eloquent relationship for hasMany has a different signature than for belongsToMany. Specifically the custom join table name-- for a system where a given Comment belongs to many Roles, and a given Role would have many Comments, I…
user101289
  • 9,888
  • 15
  • 81
  • 148
75
votes
2 answers

Laravel 5 migration identifier name too long

I am trying to run the following migration: public function up() { Schema::create('lifestyle_questions', function(Blueprint $table) { $table->increments('id'); $table->string('question'); $table->timestamps(); …
geoffs3310
  • 5,599
  • 11
  • 51
  • 104
75
votes
1 answer

Laravel get class name of related model

In my Laravel application I have an Faq model. An Faq model can contain many Product models, so the Faq class contains the following function: class Faq extends Eloquent{ public function products(){ return…
flyingL123
  • 7,686
  • 11
  • 66
  • 135
74
votes
3 answers

Eloquent: find() and where() usage laravel

I am trying to get a record from a posts database table using its id. I've been banging my head on the find() method for quite sometime now, confused as to why it wasn't working. Here is my query that looks correct to me but didn't work: $post =…
Awa Melvine
  • 3,797
  • 8
  • 34
  • 47
74
votes
9 answers

Eloquent model mass update

Please correct me if I am wrong, but I think there is no such thing as mass update in an Eloquent model. Is there a way to make a mass update on the DB table without issuing a query for every row? For example, is there a static method, something…
papas-source
  • 1,221
  • 1
  • 14
  • 20
72
votes
3 answers

Difference between Eloquent\Model::get() and all()

What is the difference between uses User::all() and User::get() on Eloquent? On Laravel API it describes only all() on Eloquent\Model.Maybe get() is described on Eloquent\Builder.
David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
72
votes
5 answers

How to check if row is soft-deleted in Eloquent?

In Laravel 5.1 is there a nice way to check if an eloquent model object has been soft-deleted? I'm not talking about selecting data but once I have the object e.g. Thing::withTrashed()->find($id) So far the only way I can see is if…
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
72
votes
5 answers

Select the first 10 rows - Laravel Eloquent

So far I have the following model: class Listing extends Eloquent { //Class Logic HERE } I want a basic function that retrieves the first 10 rows of my table "listings" and passes them on to the view (via a controller?). I know this a very…
Jonnerz
  • 1,111
  • 2
  • 11
  • 16
69
votes
3 answers

laravel updateOrCreate method

I have the following code in my method which I am sending via ajax to the controller method : $newUser = \App\UserInfo::updateOrCreate([ 'user_id' => Auth::user()->id, 'about' => $request->get('about'), 'sec_email'…
Gammer
  • 5,453
  • 20
  • 78
  • 121
69
votes
2 answers

Laravel Eloquent how to use between operator

I am trying to find an elegant way in Eloquent and Laravel to say select * from UserTable where Age between X and Y Is there a between operator in Eloquent (I can't find it). The closest i have gotten so far is chainging my query like…
GRowing
  • 4,629
  • 13
  • 52
  • 75
69
votes
12 answers

Select all from table with Laravel and Eloquent

I am using Laravel 4 to set up my first model to pull all the rows from a table called posts. In standard MySQL I would use: SELECT * FROM posts; How do I achieve this in my Laravel 4 model? See below for my complete model source…
RSM
  • 14,540
  • 34
  • 97
  • 144