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
2 answers

Eloquent: How to join a directly related table and a indirectly related table?

To demonstrate my problem, I have created a fictitious database with these 4 tables (defined as Eloquent models): User, Product, ShoppingItem (short: Item), Order; with all their relevant relations. (User included here only for…
matthiku
  • 3,610
  • 1
  • 15
  • 21
5
votes
1 answer

Laravel count Eloquent belongsToMany related model

I'm new to Laravel, and I got stuck with the following issue: I have a table for the users, and groups, and a table for connecting them. The general task any user can join any group. ---------------------------------------------- | users |…
Zoltán Fekete
  • 504
  • 1
  • 6
  • 22
5
votes
1 answer

laravel query with closure

I'm trying to query database and make a filter in a closure function, my model (Simplified) looks like this: Products: id, sale_id Sales: id, provider_id Provider: id I wanna all the products from a specific provider, so i've…
Padron Leandro
  • 313
  • 1
  • 4
  • 16
5
votes
2 answers

Laravel - eager loading a method (not a relationship) of an Eloquent model

Like we can eager load a relationship of an Eloquent model, is there any way to eager load a method which is not a relationship method of the Eloquent model? For example, I have an Eloquent model GradeReport and it has the following method: public…
Debiprasad
  • 5,895
  • 16
  • 67
  • 95
5
votes
1 answer

Laravel 5.2 Eloquent - Accessor through Scope

I have a model which I need to check values on and return back an unhealthy status. I have created an Accessor, which is working and returns true or false as expected. $task->unhealthy() Accessor code public function getUnhealthyAttribute(){ …
kayex
  • 119
  • 5
5
votes
3 answers

Skip and take all?

In eloquent, how can I skip 10 rows and then get the rest of the table? User::skip(10)->all(); The above does not work, but it gives you an idea what I am looking for.
panthro
  • 22,779
  • 66
  • 183
  • 324
5
votes
1 answer

Laravel Sort Collection By Dynamic ID array

I have the following... $people = array(5, 2, 9, 6, 11); $people_collection = People::find($people); But when I dump and die $people_collection the collection is ordered by the ID ASC, how can I keep the collection, in the same order as the…
itsliamoco
  • 1,028
  • 1
  • 12
  • 28
5
votes
2 answers

add property to each object in PHP array

My$addresses array contains multiple locations for each $client. These addresses are fetched using Eloquent relationships in Laravel. I want to get each address, discover the distance from an origin (using Google Maps API) and then add that distance…
Mike Thrussell
  • 4,175
  • 8
  • 43
  • 59
5
votes
1 answer

Laravel save Eloquent along with relationships

I added relations to a model. The structure being the following: A Structure Model hasMany Managers. Each Manager hasMany Employees So I get this done using the following code: $structure->name = "Something"; $manager1 = new Manager; $manager2 =…
user6614376
5
votes
4 answers

Items filter laravel eloquent

What is the best way to implement the items filter functionality? I have a table with some items, and each item has some fields. How can I select items with fields filtering, like e-commerce filtering, using Laravel 5 and Eloquent?
aGoodRussianGuy
  • 173
  • 1
  • 1
  • 11
5
votes
2 answers

Laravel exclude current id from query eloquent results

I am fairly new to laravel and I built a little "similar posts" section. So every post has a tag and I query all the id's from the current tag. And then I find all the posts with thoses id's. Now my problem is that the current post is always…
Cruzito
  • 338
  • 3
  • 7
  • 18
5
votes
2 answers

Eloquent morphTo()->withTrashed() stopped working

I have a polymorphic relationship set up in an OrderItem model, where saleable can be a few different models. I've set it up like any other relationship: public function saleable() { return $this->morphTo()->withTrashed(); } This used to work…
Wasim
  • 4,953
  • 10
  • 52
  • 87
5
votes
1 answer

How to update time to NOW()?

I want to set a date field to NOW() if it's outdated. Here is the query I'm trying to run without success: Quiz::('date', '<', DB::raw('NOW()'))->update(['date' => DB::raw('NOW()')]) How do I fix it? P.S I don't want to deal with Carbon, if…
Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
5
votes
1 answer

Laravel Left join add where clause to right table

If I add a where clause to a left join, that this where clause is working on the right table, I only get those results frome left table that can match the right table. $notifications = \DB::table('notifications') …
Salar
  • 5,305
  • 7
  • 50
  • 78
5
votes
1 answer

Laravel 5.2 Eloquent create() method shows foreign key exception but save() doesn't show any error

Here is the situation: I have two tables users and projects. Users table has one to many relation with projects table. When I try to insert data in projects table by using Model::create() method, it shows foreign key constraint error. But when I…
Jitender Singla
  • 133
  • 1
  • 12