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

Laravel Jetstream - How to join a default team at registration

I am trying to change Laravel Jetstream's logic in order to join a default team at registration rather than create a personal team. I found the method that does this: public function create(array $input) { Validator::make($input, [ …
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
4
votes
3 answers

Laravel Eloquent limit() and count() returns full count

I need to use the count() method on the latest 1000 records. Example, we have 3 users: User A with 900 records User B with 20.000 records User C with 10 records Now I want to consider only the latest 1000 records for my calculation, which means…
Roman
  • 3,563
  • 5
  • 48
  • 104
4
votes
2 answers

Laravel Eloquent - Filter Relationship

I have two main models in my Laravel project. ProductMaster is where the main product data is and Product is the model which holds the variation of each product and the price for each client. I am not allowed to change this model, in case you…
4
votes
2 answers

How to merge child key pairs to parents in Laravel models

Old PHP user here but new to Laravel, I need help(answers or link to related solution)... If I have a relation model(or collection, I get confused here)... $data = MyData::with('new_data')->first(); resulting with... data = { a: 1, b: 2, …
Jacobski
  • 741
  • 6
  • 10
4
votes
1 answer

how to get updated records ids in laravel?

I am updating multiple rows in laravel . StoragePeriod::where('customer_id', $invoice->customer_id)->update(['invoice_id' => $invoice->id]); How I can get ids of those updated records?
user13815476
4
votes
1 answer

Laravel Eager Load with conditional statement

good day everyone I'm new to Laravel and currently building my first simple news app with it I have a simple query that using laravel's eager loading function But in this eager loading function, I want to fetch certain comments based on some…
Gwein
  • 109
  • 3
  • 14
4
votes
1 answer

How to update image with PUT method in Laravel REST API?

I am trying to build a REST API with Laravel where users need to update their image. But, If I use PUT method in postman it does not update the image or store the image in designated folder. If I use POST instead it does update the image and also…
Jakaria Ridoy
  • 142
  • 2
  • 9
4
votes
3 answers

Laravel Eloquent - pluck() role name

I'm trying to get the authenticated user object from the request with roles. I'm using Spatie laravel-permissions and Laravel 8. Getting User object from request like so $request->user()->getRoleNames()->pluck('name'); return…
Josh Bonnick
  • 2,281
  • 1
  • 10
  • 21
4
votes
2 answers

created_by & updated_by in Laravel's updateOrCreate

With "updateOrCreate" command, how do we update "created_by" when insert new rows, and update "updated_by" when it updates? $flight = App\Flight::updateOrCreate( [ 'departure' => 'Oakland', 'destination' => 'San Diego' ], …
Heru Handoko
  • 79
  • 1
  • 10
4
votes
2 answers

How can I know if a table column is a foreign key in Laravel through Model?

So as the title says, how can I know if a field of a Model is a foreign key in Laravel ? Supose I have a FK column called show_type_id and a model named Event and I want to know if there is a function that given the model class or model table and…
didix16
  • 83
  • 1
  • 10
4
votes
4 answers

Laravel API not accepting JSON request from Postman

Laravel API is not accepting JSON request. If I request as form-data it works but if I post JSON object in postman body then it does not receive the request data. Route: $router->group(['prefix' => 'imp'], function () use ($router) { …
Imdad Ali
  • 727
  • 1
  • 8
  • 18
4
votes
0 answers

Eloquent eager loading relationship

Hello wonderful people of StackOverflow, I hope you all have a good day ( ˘⌣˘)♡(˘⌣˘ ) I'm new to Laravel and currently learning about eloquent relationships (hasMany) I'm sorry this post was too long to read, but I wanted to clarify every step I…
Gwein
  • 109
  • 3
  • 14
4
votes
4 answers

Eloquent - Compare attribute changes after save() model

I'm building an audit system in my application, and I want to compare an Eloquent Model attribute changes after save() method. Here's an example of what I need: $person = Person::find(1); //Original data: $person->name -> 'Original name',…
Fantasmic
  • 1,122
  • 17
  • 25
4
votes
1 answer

Get the $request object in Laravel's model events (ex. created, updated etc.)

I am using Eloquent's model events to do some actions after model has been created/updated/deleted. I'm refering to these: class User extends Model { public static function boot() { parent::boot(); …
Ivica Pesovski
  • 827
  • 7
  • 29
4
votes
2 answers

Laravel group by with select all column

Is there a way on laravel eloquent to query like this: select users.* from users group by name to implement it on code: Users::select('name')->groupBy('name')->get(); Now I need to get other details (columns) of the user. Is there a way to query…
mpalencia
  • 5,481
  • 4
  • 45
  • 59
1 2 3
99
100