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

Eloquent Many to Many select User without certain Roles

So I have User & Role models with many-to-many relationship, I have 3 roles: super, admin and moderator with 4 users let's says: John, Mike, James and Larry. John is a super, Mike has admin and moderator roles, James is an admin and Larry is a…
Rifki
  • 3,440
  • 1
  • 18
  • 24
5
votes
2 answers

Laravel 4.2: Copying database records from one database to another

I need to copy a subset of records from one database to another in Laravel 4.2 I've written an artisan task that loads the models that I need to copy from the "default" database connection, and now need to save them to the second database …
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
5
votes
1 answer

How to insert multiple records to database using Laravel Eloquent

I am developing a web application , there comes a scenario where user can insert multiple records at once.I created a Form at route result/create where user can add multiple row in front end , now when the user click on submit i want all the…
user6060368
5
votes
1 answer

Laravel Password resets for multiple auth

i have a second auth for companies which is working. But the only thing i have a problem with is sending the password token email for companies. It always sends the email from users. That´s my app/config/auth.php If i change on defaults the…
Wolfgang Müller
  • 386
  • 1
  • 6
  • 21
5
votes
1 answer

Laravel Eloquent toArray not using square braces

I'm writing an api and attempting to convert a number of results into JSON. When the eloquent result is converted to an array, I am expecting something like this: [ { "id": "0", ... }, { "id": "", ... }, ] but instead, Laravel…
Deal Seal
  • 53
  • 5
5
votes
1 answer

Laravel Eloquent Relationship - Inserting data into multiple tables

I have the following structure: 3 tables: movies, actors, genres Movies Table Schema: Schema::create('movies', function (Blueprint $t) { $t->increments('id'); $t->string('title'); $t->integer('genre_id')->unsigned(); …
Daybreak
  • 165
  • 1
  • 2
  • 9
5
votes
2 answers

Using a Laravel model method in an Eloquent query

This is for Laravel 5.2. I have a method defined as follows in my Users model: public function name() { return "$this->name_first $this->name_last"; } I'm trying to figure out how to use that as part of a query, but it seems like it isn't…
Anthony
  • 1,760
  • 1
  • 23
  • 43
5
votes
1 answer

Laravel dynamic relationships - access model attributes on eager load

I have an Eloquent relationship on my Laravel model which is dynamic - that is, the value of a particular database field determines which model will get loaded. I am able to load this relationship fine when I first instantiate the model instance and…
Andy Noelker
  • 10,949
  • 6
  • 35
  • 47
5
votes
4 answers

Laravel 5 eloquent hasManyThrough / belongsToManyThrough relationships

In a Laravel 5.2 application I have three models: User, Role and Task. A User is associated with multiple Roles, and a Role is associated with multiple Tasks. Therefore each user is associated to multiple tasks, through their roles. I am trying to…
datavoredan
  • 3,536
  • 9
  • 32
  • 48
5
votes
2 answers

Transaction with Eloquent Laravel 5

I am using MyISAM for MySQL and I want to use transaction here is my code: DB::transaction(function () { $project = Project::find($id); $project->users()->detach(); $project->delete(); }); This code execute succesfuly but I am not sure…
Vladimir Djukic
  • 2,042
  • 7
  • 29
  • 60
5
votes
1 answer

How to get data from intermediate table in eloquent, silex

I created database tables users, groups, and group_user (MySQL).And group_user table (intermediate table) contains the user_id and role_id. users and groups relationship is many to many. I want to delete a group in groups table. Before deleting a…
Janaka Pushpakumara
  • 4,769
  • 5
  • 29
  • 34
5
votes
3 answers

How to use 'hasManyThrough' with more than 3 tables in Laravel?

as stated in the docs the current hasManyThrough can be used when u have something like country > users > posts which result in something like Country::whereName('xx')->posts; which is great but what if i have more than that like country > cities >…
ctf0
  • 6,991
  • 5
  • 37
  • 46
5
votes
1 answer

How can I use whereHas on a relation to the same table with Eloquent Query Builder?

I'm trying to run this query: $products = $this->product_model->where(function($query) use ($key) { $query->whereHas('categories', function ($category) use ($key) { $category->where('key', $key); }); …
Harry Potts
  • 168
  • 2
  • 10
5
votes
2 answers

Is there a way to mass assign relations in laravel 5?

I have two Model classes with a oneToMany relation like this App\Car class Car extends Model { public $timestamps = true; protected $fillable = [ 'name', 'price' ]; public function parts () { return…
L. Catallo
  • 563
  • 4
  • 11
5
votes
2 answers

laravel firstOrCreate not working properly?

I am importing some data with the foursquare api. My database contains multiple foursquare_id duplicates. Am I doing something wrong with the code here? I thought the way this is set up it will check the database for the column value of…
Stephan-v
  • 19,255
  • 31
  • 115
  • 201