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
46
votes
5 answers

Laravel Eloquent sort by relation table column

I tried to sort products from shop_products table by pinned column from shop_products_options table: $products = Shop\Product::with(['options' => function ($query) { $query->orderBy('pinned', 'desc'); }])->paginate(5); I set relation in…
Sasha
  • 764
  • 1
  • 13
  • 21
45
votes
8 answers

How to search case insensitive in Eloquent model

I want to search case-insensitive in Eloquent model. Now I am using this Model::where($column, 'LIKE', '%' . $value . '%' ); But it is case sensitive. How can I solve this? I also find this post How can I search (case-insensitive) in a column…
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
45
votes
5 answers

How to update a pivot table using Eloquent in laravel 5

I am new to laravel. I am working on a laravel 5 app and I am stuck here. I have 2 models as such: class Message extends Eloquent{ public function user() { return $this->belongsTo('App\User', 'from'); } public function…
Fokwa Best
  • 3,322
  • 6
  • 36
  • 51
45
votes
3 answers

laravel eloquent relationship with and where clause based on foreign column

Hi I want to retrieve my projects held in a db which are owned by an auth user who also creates clients (they have many projects and tasks) and tasks (belongs to a project and tasks and user). I want to retrieve all tasks that are not marked as…
user0129e021939232
  • 6,205
  • 24
  • 87
  • 140
45
votes
5 answers

Laravel Eloquent: How to order results of related models?

I have a model called School and it has many Students . Here is the code in my model: public function students() { return $this->hasMany('Student'); } I am getting all the students with this code in my controller: $school =…
Debiprasad
  • 5,895
  • 16
  • 67
  • 95
44
votes
4 answers

How to alias the name of a column in Eloquent

I have an eloquent model named Eloquent: Products::where("actice", "=", true)->get()->toArray(); Now I want to add join-statement to it, I have defined a scopeQuery with: public function scopeJoinWithTags($query) { return…
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
43
votes
9 answers

Order by relationship column

I have the following query: $items = UserItems::with('item') ->where('user_id','=',$this->id) ->where('quantity','>',0) ->get(); I need to order it by item.type so I tried: $items = UserItems::with('item') …
TheUnreal
  • 23,434
  • 46
  • 157
  • 277
43
votes
2 answers

Laravel belongsToMany exclude pivot table

I have two models, User and Badge. A user can have multiple badges, and a badge can belong to multiple users. (using a pivot table) Currently I am getting the data I need, but additionally I am getting the pivot table along. How do I exclude…
Patrick Reck
  • 11,246
  • 11
  • 53
  • 86
43
votes
10 answers

Dynamically hide certain columns when returning an Eloquent object as JSON?

How do dynamically hide certain columns when returning an Eloquent object as JSON? E.g. to hide the 'password' column: $users = User::all(); return Response::json($users); I'm aware I can set protected properties in the model ($hidden or $visible),…
mtmacdonald
  • 14,216
  • 19
  • 63
  • 99
43
votes
3 answers

Laravel Eloquent Select CASE?

Is there anyone with experience in PHP & Laravel Eloquent who can help me resolve this statement? I'm trying to inject a CASE... WHEN.. END... inside a raw() method. It seemed like it was completely ignored. The existing documentation hasn't been .…
Coach Roebuck
  • 904
  • 2
  • 12
  • 20
43
votes
3 answers

How to use Eloquent ORM without Laravel?

Is it possible to use Eloquent without Laravel or does somebody knows a equally easy to use ORM?
Andre Zimpel
  • 2,323
  • 4
  • 27
  • 42
42
votes
3 answers

How to define and use JSON data type in Eloquent?

How can I define JSON data type that is provided in pgsql 9.4 in Laravel 5? I need to define data type, storing and fetching data and so far could not find way to deal it in Laravel 5.1
Volatil3
  • 14,253
  • 38
  • 134
  • 263
42
votes
5 answers

Move Laravel 5 Eloquent models into its own directory

Laravel 5 has ORM models by default in app folder. I want to move these into app/models. When I do that then these classes are not found anymore. How to make Laravel find the Eloquent ORM models from app/models?
Margus Pala
  • 8,433
  • 8
  • 42
  • 52
42
votes
4 answers

Laravel Custom Model Methods

Whenever I add additional logic to Eloquent models, I end up having to make it a static method (i.e. less than ideal) in order to call it from the model's facade. I've tried searching a lot on how to do this the proper way and pretty much all…
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
42
votes
2 answers

Storing array or std object in database of Laravel app

What is the schema for the database table to store Array or stdObject in a column ? When I use ->string and insert an array variable, it only stores the word "Array" in the db column. does Laravel has support for array? If no, What methodology…
rgb
  • 533
  • 1
  • 6
  • 12