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
5
votes
1 answer

Laravel-5 Separate model for each log table

I am currently developing an application with Laravel 5, my main tables are users, suppliers, manufacturers etc. Each of these tables has a separate users_log,suppliers_log log table. The purpose of these log tables is to review operations…
Magesh Kumaar
  • 1,485
  • 2
  • 10
  • 29
5
votes
2 answers

Laravel Eloquent nested query

I was working with Laravel and got stuck in a situation. I have following models: Category Product CategoryProduct CategoryProduct holds the information about which product belongs to which category (a product may belong to multiple…
Ashutosh
  • 4,371
  • 10
  • 59
  • 105
5
votes
2 answers

Does Laravel query database each time I call Auth::user()?

In my Laravel application I used Auth::user() in multiple places. I am just worried that Laravel might be doing some queries on each call of Auth::user() Kindly advice
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
5
votes
2 answers

How to join models in Laravel 5?

I have three tables, with a structure like this (they are in an MySQL DB) connected to my Laravel 5 API with Eloquent models: # build_sets table | id | title | description | # parts table | id | title | description | color | # build_set_parts…
KungWaz
  • 1,918
  • 3
  • 36
  • 61
5
votes
1 answer

Laravel 5 Get DB Prefix

In Laravel 4 I could do this to get the table prefix: $prefix = DB::getTablePrefix(); What's the equivalent in L5?
sterfry68
  • 1,063
  • 2
  • 14
  • 30
5
votes
1 answer

laravel 4 updateExistingPivot with where clause not working

I have two models: user and resource And relation table is resource_user. the fields in resource_user are: id | resource_id | user_id | another_id I have this relation in user: public function resources() { return…
goldlife
  • 1,949
  • 3
  • 29
  • 48
5
votes
3 answers

Laravel order by conditions eloquent or sql

I have these timestamp columns in database table - expiration, edit, created_at. I need to order by 'edit', if item 'expiration' date is bigger than today's date, else I need to order by 'created_at'. I am trying something like this, but it isn't…
user3921996
  • 149
  • 1
  • 1
  • 9
5
votes
2 answers

Get posts by followers laravel

I want to display a feed page for an authenticated user which shows the latest posts from users that they follow. I have a follow system set up already which has the following: Tabels: Posts users follow User model: public function follow() { …
John
  • 196
  • 7
5
votes
2 answers

Laravel 4.2 returning soft deleted on BelongsToMany results

Hello I just noticed one weird behaviour of softDelete. Basically, when I query for a related set of models, Eloquent returns a collection that also contains soft deleted rows. I have been following the 4.2 guides on traits usage for softdelete and…
Federico Stango
  • 548
  • 5
  • 21
5
votes
1 answer

How to save NOW() to the same field in my Laravel Eloquent model every time I save?

Every time I perform the following: $user = new User; $user->name = 'John'; $user->save(); I want a field called "modified" to be updated to NOW() in MySQL. I can't use Laravel's timestamps because I'm only using a single modified field. I could…
prograhammer
  • 20,132
  • 13
  • 91
  • 118
5
votes
1 answer

Laravel 4 Eager Loading constraints

I want to get all Items (topics) WITH their comments, if comments user_id = $id. I try something like this, but it isn't working. So if Item hasn't got any comment with user_id = $id, then I don't need this Item. In DiscussionsItem model I have…
user3921996
  • 149
  • 1
  • 1
  • 9
5
votes
1 answer

Laravel Eloquent: getting id field of joined tables in Eloquent

I need to get the id of a second table. I cannot avoid using a INNER JOIN since I have to ORDER the results by a column in the second table. Here are my 2 tables events id name .... date ... myevents id meeventID meresults .... To…
Edwin Krause
  • 1,766
  • 1
  • 16
  • 33
5
votes
2 answers

How can I add an item into a Laravel Eloquent Collection by index?

I tried the following but it doesn't work. $index = 2; $collection->put($index, $item4); For example if $collection looks like this: $collection = [$item1, $item2, $item3]; I'd like to end up with: $collection = [$item1, $item2, $item4, $item3];
Howard
  • 3,648
  • 13
  • 58
  • 86
5
votes
0 answers

Get the first element of a relationship with Eloquent

I would know if it's possible to retrieve the first element of relation with Eloquent. This doesn't work: $posts = Post::with(['medias' => function($q) { $q->where('type', 'landscape') // not working ->limit(1); // not working …
KeizerBridge
  • 2,707
  • 7
  • 24
  • 37
5
votes
1 answer

Reserved word on eloquent models

I have a Product model and a Attribute model. Products have Attributes. Problem is I cant use Attributes as a member of a Product class that extends eloquent as eloquent uses this already. Is there a way around this? class Product extends Model { …
AndrewMcLagan
  • 13,459
  • 23
  • 91
  • 158