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
66
votes
8 answers

Seed multiple rows at once laravel 5

I'm currently trying to seed my users table. If I try it like this with 2 rows, it fails. It works fine if I just use a single array instead of the 2 arrays inside the $users array to create some fake data. What am I doing wrong, what is the proper…
Stephan-v
  • 19,255
  • 31
  • 115
  • 201
66
votes
5 answers

Laravel Eloquent: Return Array key as the fields ID

When I execute a query via Laravel's Eloquent ORM, I want to get the row ID as the Array result key, this will make things easier when I want to check something based on an ID (using array_key_exists) or do some look ups (if I need a specific entry…
Broshi
  • 3,334
  • 5
  • 37
  • 52
66
votes
7 answers

laravel select where and where condition

I have this basic query i want to perform but an error keeps coming up. Probably due to my newness to laravel. here is the code: $userRecord = $this->where('email', $email)->where('password', $password); echo "first name: " .…
spacemonkey
  • 2,530
  • 6
  • 23
  • 27
66
votes
3 answers

Eloquent push() and save() difference

I have read laravel 4 docs about eloquent and was quite intrigued by the push() part. It says, Sometimes you may wish to save not only a model, but also all of its relationships. To do so, you may use the push method: Saving A Model And…
Melvin
  • 5,798
  • 8
  • 46
  • 55
65
votes
4 answers

What is the difference between BelongsTo And HasOne in Laravel

Can any body tell me what is the main difference between the BelongsTo and HasOne relationship in eloquent.
Pardeep Pathania
  • 1,378
  • 2
  • 15
  • 23
65
votes
6 answers

Eloquent collections: each vs foreach

Might not be a question specific to Eloquent collections, but it just hit me while working with them. Let's just assume we have a $collection object which is an instance of Illuminate\Support\Collection. Now if we want to iterate over it, what are…
Niklas Modess
  • 2,521
  • 1
  • 20
  • 34
64
votes
5 answers

laravel migration re-organising column order

When you create a new column in a table you can use the ->after('column name') to dictate where it goes. How can I create a migration that re-orders the columns in the right order I want?
user391986
  • 29,536
  • 39
  • 126
  • 205
64
votes
5 answers

How to check if a record is new in Laravel?

I recently started using Eloquent. When I used PHP Active Record, there was a nice function that checked if a record was loaded from the database or is a new instance. Is there something similar in Eloquent that I could use? By new I mean: $article…
Craig Ward
  • 2,425
  • 5
  • 33
  • 51
62
votes
5 answers

How to update column value in laravel

I have a page model. It has following columns in database table: id title image body I want to update only "image" column value. Here's my code: public function delImage($path, $id) { $page = Page::find($id); $page->where('image',…
Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
62
votes
18 answers

How to return database table name in Laravel

Is there a way that I can get the current database table in use by the model that I'm in? I see that there is a table() function in Laravel/Database/Eloquent/model.php but I've been unsuccessful calling it calling it from the model that I'm in.
Chris G
  • 6,700
  • 2
  • 18
  • 20
60
votes
7 answers

Truncate a table in Laravel 5

Description : I have a table full of tested data. Sometimes, I want to clear it out for new data. I can perform the truncate in the DBMS App like MySQL WorkBench, but I'm trying to achieve it within my application instead. Goal : to make a button…
code-8
  • 54,650
  • 106
  • 352
  • 604
59
votes
6 answers

How to set a default attribute value for a Laravel / Eloquent model?

If I try declaring a property, like this: public $quantity = 9; ...it doesn't work, because it is not considered an "attribute", but merely a property of the model class. Not only this, but also I am blocking access to the actually real and…
J. Bruni
  • 20,322
  • 12
  • 75
  • 92
58
votes
9 answers

Laravel Recursive Relationships

I'm working on a project in Laravel. I have an Account model that can have a parent or can have children, so I have my model set up like so: public function immediateChildAccounts() { return $this->hasMany('Account', 'act_parent',…
Troncoso
  • 2,343
  • 3
  • 33
  • 52
58
votes
3 answers

Eloquent column list by key with array as values?

So I can do this with Eloquent: $roles = DB::table('roles')->lists('title', 'name'); But is there a way to make Eloquent fetch an array of values for each distinct key instead of just one column? For instance, something like the following: $roles =…
eComEvo
  • 11,669
  • 26
  • 89
  • 145
57
votes
1 answer

Laravel Model with Two Primary Keys update

I'm trying to update Model which has two primary keys. Model namespace App; use Illuminate\Database\Eloquent\Model; class Inventory extends Model { /** * The table associated with the model. */ protected $table =…
Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42