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
269
votes
3 answers

Laravel 4: how to "order by" using Eloquent ORM

Simple question - how do I order by 'id' descending in Laravel 4. The relevant part of my controller looks like this: $posts = $this->post->all() As I understand you use this line: ->orderBy('id', 'DESC'); But how does that fit in with my above…
Josh
  • 5,999
  • 8
  • 30
  • 43
265
votes
24 answers

How to select specific columns in laravel eloquent

lets say I have 7 columns in table, and I want to select only two of them, something like this SELECT `name`,`surname` FROM `table` WHERE `id` = '1'; In laravel eloquent model it may looks like this Table::where('id', 1)->get(); but I guess this…
devnull Ψ
  • 3,779
  • 7
  • 26
  • 43
258
votes
22 answers

Select Last Row in the Table

I would like to retrieve the last file inserted into my table. I know that the method first() exists and provides you with the first file in the table but I don't know how to get the last insert.
Carlos Suñol
  • 2,643
  • 2
  • 15
  • 10
254
votes
41 answers

Migration: Cannot add foreign key constraint

I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error: [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter…
user0129e021939232
  • 6,205
  • 24
  • 87
  • 140
233
votes
12 answers

Bulk Insertion in Laravel using eloquent ORM

How can we perform bulk database insertions in Laravel using Eloquent ORM? I am working with an XML document, looping through its elements. I want to accomplish something like this in Laravel: $sXML =…
phoenixwizard
  • 5,079
  • 7
  • 27
  • 38
221
votes
5 answers

What does "Mass Assignment" mean in Laravel?

When I went through Laravel Document about Eloquent ORM topic part, I got a new term "Mass Assignment". Document show How to do Mass Assignment and the $fillable or $guarded properties settings. But after went through that, I didn't have a clearly…
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
217
votes
3 answers

How to insert multiple rows from a single query using eloquent/fluent

I have the following query: $query = UserSubject::where('user_id', Auth::id())->select('subject_id')->get(); and as expected I get the following result: [{"user_id":8,"subject_id":9},{"user_id":8,"subject_id":2}] Is there a way of copying the…
Billy
  • 2,823
  • 3
  • 16
  • 33
216
votes
18 answers

Automatically deleting related rows in Laravel (Eloquent ORM)

When I delete a row using this syntax: $user->delete(); Is there a way to attach a callback of sorts, so that it would e.g. do this automatically: $this->photo()->delete(); Preferably inside the model-class.
Martti Laine
  • 12,655
  • 22
  • 68
  • 102
213
votes
13 answers

Laravel Eloquent "WHERE NOT IN"

I'm having trouble to write query in laravel eloquent ORM. my query is SELECT book_name,dt_of_pub,pub_lang,no_page,book_price FROM book_mast WHERE book_price NOT IN (100,200); Now I want to convert this query into laravel eloquent.
Nur Uddin
  • 2,778
  • 6
  • 16
  • 22
210
votes
14 answers

How to delete all the rows in a table using Eloquent?

My guess was to use the following syntax: MyModel::all()->delete(); But that did not work. I'm sure it's super simple, but I've searched for documentation on the subject and can't find it!
Pete
  • 7,289
  • 10
  • 39
  • 63
200
votes
10 answers

How to alias a table in Laravel Eloquent queries (or using Query Builder)?

Lets say we are using Laravel's query builder: $users = DB::table('really_long_table_name') ->select('really_long_table_name.id') ->get(); I'm looking for an equivalent to this SQL: really_long_table_name AS short_name This…
prograhammer
  • 20,132
  • 13
  • 91
  • 118
198
votes
11 answers

Laravel Check If Related Model Exists

I have an Eloquent model which has a related model: public function option() { return $this->hasOne('RepairOption', 'repair_item_id'); } public function setOptionArrayAttribute($values) { $this->option->update($values); } When I create the…
Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59
190
votes
5 answers

Laravel orderBy on a relationship

I am looping over all comments posted by the Author of a particular post. foreach($post->user->comments as $comment) { echo "
  • " . $comment->title . " (" . $comment->post->id . ")
  • "; } This gives me I love this post (3) This is a comment…
    PrestonDocks
    • 4,851
    • 9
    • 47
    • 82
    188
    votes
    21 answers

    Get the Query Executed in Laravel 3/4

    How can I retrieve the raw executed SQL query in Laravel 3/4 using Laravel Query Builder or Eloquent ORM? For example, something like this: DB::table('users')->where_status(1)->get(); Or: (posts (id, user_id,…
    Patrick Maciel
    • 4,874
    • 8
    • 40
    • 80
    186
    votes
    15 answers

    Creating and Update Laravel Eloquent

    What's the shorthand for inserting a new record or updating if it exists? where('metadataKey', '=', 2001)->first(); if ($shopOwner == null) { // Insert new record into database }…
    1myb
    • 3,536
    • 12
    • 53
    • 73