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

Get primary key of Eloquent model instance

I'm happy enough to work on a project with (not) very consistent column names in our MySQL database. We have camelCase, PascalCase, 'Id', ID and it's driving me nuts. I've decided to start using Eloquent in our application (which is NOT Laravel). I…
Basaa
  • 1,615
  • 4
  • 20
  • 41
36
votes
5 answers

Laravel, How to use where conditions for relation's column?

I'm using Laravel and having a small problem with Eloquent ORM.. I can get this working simply with SQL query using a JOIN but I can't seem to get it working with Eloquent! This is what I want, I have two tabels. one is 'Restaurants' and other is…
Tharshan Venkdesan
  • 363
  • 1
  • 3
  • 7
35
votes
4 answers

Laravel eloquent with Trashed on relationship

I need to be able to get a Models Relationship including its soft deleted elements, but only for this 1 instance. I do not want to change the model so that every time I use the relationship it returns all the soft deleted records too. How can I…
S_R
  • 1,818
  • 4
  • 27
  • 63
35
votes
5 answers

Update Table Using Laravel Model

I've got a table for a sports team. The record shows the team selection and some other information. I want to update the record with the team selection. My model is thus: class Selection extends Model { protected $table = "selection"; protected…
danjswade
  • 557
  • 2
  • 8
  • 16
35
votes
6 answers

Get Laravel Models with All Attributes

Is there a way to retrieve a model in Laravel with all the attributes, even if they're null? It seems to only return a model with the attributes that aren't null. The reason for this is that I have a function that will update the model attributes…
kenshin9
  • 2,215
  • 4
  • 23
  • 38
35
votes
4 answers

Laravel 5: cascade soft delete

I am having offers and services table. Service(s) is a child of an offer. So far I have established functionality for soft deleting an offer. How would I also soft delete appended services? Here is my code: Migration Offers Schema::create('offers',…
be-codified
  • 5,704
  • 18
  • 41
  • 65
35
votes
6 answers

Laravel: dynamic where clause with Elouquent

I am calling URL with search params which are dynamic. How could I form proper Eloquent query? In theory: query query where(someParam1) query where(someParam2) query orderby(someParam3) query get I need this kind of structure so I can use where…
be-codified
  • 5,704
  • 18
  • 41
  • 65
35
votes
2 answers

Where and If Statements Laravel Eloquent

I have built a multi filter search for my website but I cant seem to find any documentation on multiple if statements inside of where for my search. Returns Lots of Results $data = Scrapper::paginate(15); Returns none.. need it to be this way to…
Brent
  • 2,385
  • 10
  • 39
  • 63
35
votes
3 answers

How to update the updated_at column when the user logs in?

I'm trying to update the updated_at column to the current time, each time a user logs in. But I get the following error: InvalidArgumentException A four digit year could not be found Data missing PHP $input = Input::all(); $remember =…
Xolmer
  • 375
  • 1
  • 4
  • 6
35
votes
9 answers

Mocking Laravel Eloquent models - how to set a public property with Mockery

I want to use a mock object (Mockery) in my PHPUnit test. The mock object needs to have both some public methods and some public properties set. The class is a Laravel Eloquent model. I tried this: $mock =…
mtmacdonald
  • 14,216
  • 19
  • 63
  • 99
35
votes
5 answers

how can I create a migration to add a value to an enum in eloquent

I have a table that contains an enum field CREATE TABLE `user_status` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `values` enum('on', 'off'), PRIMARY KEY (`id`), ) ENGINE=InnoDB; how can I create a migration to add a value to the enum…
user391986
  • 29,536
  • 39
  • 126
  • 205
35
votes
3 answers

Laravel eloquent counting a relation

I'm new to laravel and eloquent and I'm not sure if this is even possible. but I have 2 tables with a one to many relationship. One is "locations" and one is "users". One location can have many users. So if I wanted to get all locations with all…
Arcade
  • 736
  • 2
  • 7
  • 17
34
votes
7 answers

Method orderBy does not exist in Laravel Eloquent?

I have a piece of code like this: $products = Product::all() if ($search_value) { $products = $products->where('name', 'LIKE', "%$search_value%"); } $products = $products->orderBy('created_at',…
Harrison
  • 2,560
  • 7
  • 29
  • 55
33
votes
6 answers

Laravel 5.1 Create or Update on Duplicate

In Laravel 5.1, for MySQL insert, I want to see if the record already exists and update on duplicate or create new if none exists. I have already searched SO where the answers were for old laravel versions. In one of the old topics it said that a…
Neel
  • 9,352
  • 23
  • 87
  • 128
33
votes
2 answers