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
42
votes
6 answers

Find or Create with Eloquent

I have recently started working with Laravel and Eloquent, and was wondering about the lack of a find or create option for models. You could always write, for example: $user = User::find($id); if (!$user) { $user = new User; } However, is there…
charleyh
  • 2,033
  • 2
  • 20
  • 32
41
votes
1 answer

Instance the query builder directly from model

When I do something like SomeModel::with('user') it returns a Query\Builder instance. How can I get this instance without need call the with() (or similar)? For instance, I tried it: new SomeModel, but it'll returns obviously the instance of my…
David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
41
votes
2 answers

A __construct on an Eloquent Laravel Model

I have a custom setter that I'm running in a __construct method on my model. This is the property I'm wanting to set. protected $directory; My Constructor public function __construct() { $this->directory =…
Nathan Lochala
  • 495
  • 1
  • 4
  • 9
41
votes
10 answers

how get random row laravel-5

In L-4 it was simple: $random_quote = Quotation::all()->random(1); But now in L-5 not a single method described in this post is working: Laravel - Eloquent or Fluent random row My view file just gets blank.
Peter
  • 2,634
  • 7
  • 32
  • 46
41
votes
3 answers

Laravel Eloquent: sum with groupBy

Need eloquent/fluent query to get sum with groupBy function. So far I have tried: $this->data['no_of_pages'] = Document::sum('no_of_pages') ->groupBy('users_editor_id'); Which ofcourse gives me call to member…
user2067888
  • 1,085
  • 3
  • 11
  • 17
41
votes
5 answers

Laravel attach pivot to table with multiple values

Background I'm creating a database revolving around food allergies and I have a many to many relationship between foods and allergies. There is also a pivot value called severity which has a numerical number representing the severity of the allergy…
Duncan Ogle
  • 784
  • 2
  • 7
  • 17
40
votes
14 answers

Laravel Eloquent $model->save() not saving but no error

When updating my Post model, I run: $post->title = request('title'); $post->body = request('body'); $post->save(); This does not update my post. But it should according to the Laravel docs on updating Eloquent models. Why is my model not being…
Jacob
  • 1,560
  • 4
  • 19
  • 41
40
votes
1 answer

Laravel 5.3 withCount() nested relation

The model structure is as follows Tutorial -> (hasMany) Chapters -> (hasMany) videos How can we load number of videos (video_count) from Tutorial Model with laravel 5.3's withCount() method I have…
Sahil Deliwala
  • 802
  • 1
  • 7
  • 14
40
votes
13 answers

Laravel 5.1 DB:select toArray()

I have a large SQL statement that I am executing like so: $result = DB::select($sql); For example $result = DB::select('select * from users'); I'd like the result to be an array - but at the moment it returns a structure like so, an array with…
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
40
votes
4 answers

Laravel Distinct Count

Any way to make this query work using laravel? DB::raw or Eloquent usage doesn't matter. SELECT count(DISTINCT name) FROM tablename; Here's what i've tried but cannot get the proper output: EloquentTableName::select(DB::raw('count(DISTINCT name) as…
saimcan
  • 1,706
  • 7
  • 32
  • 64
40
votes
3 answers

Nested 'AND OR' Query in Eloquent

I'm currently attempting to create a nested query, as follows: public function getChallenge($user_id, $opponent_id) { $challenge = $this->challenges() ->where('open', true) ->where(function($query) use ($user_id,…
sndrsnk
  • 488
  • 1
  • 4
  • 11
39
votes
3 answers

Laravel: orderBy a column with collections

I need to OrderBy a column with collection. I need to orderBy(updated_at, 'desc') all posts which owned by current logged user. Here is my code : $posts = auth()->user()->posts->sortByDesc('updated_at'); Here is User model : class User extends…
Hamed Kamrava
  • 12,359
  • 34
  • 87
  • 125
39
votes
4 answers

Call to undefined method Illuminate\Database\Query\Builder::lists() when seeding after updating to Laravel 5.3

I'm updating to laravel 5.3, and I get this message: [2016-08-23 23:12:39] local.ERROR: BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::lists() in…
Juliatzin
  • 18,455
  • 40
  • 166
  • 325
39
votes
4 answers

How to change Laravel model's table name

I am using Laravel 5 and have changed the name of a database table from "domain_related_settings" to "DomainRelatedSettings" by rolling back all migrations, changing the specific migration, and running them again. The new table name is reflected in…
user5512902
  • 473
  • 1
  • 4
  • 12
39
votes
2 answers

Laravel hasMany with where in a Polymorphic relationship

I have 3 tables, Cars, Flats and Shops. Each table has its photos. Photos is stored in database. I want to use only one table for photos, I don't want to create Photos table for each Cars, Flats and Shops. Photos tables structe is like this; | id | …
fobus
  • 1,938
  • 8
  • 29
  • 48