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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
22 views

Join on multiple tables from different db in Laravel

I decided to separate tables into different databases and now i can't make a join query on different tables I added connectin property to each model in my project so now Account model has protected $...
user483882's user avatar
-1 votes
1 answer
48 views

Why model file load first than config file in laravel module

I have a problem with model and config file, I was trying to call the config in the model. the code was like this in the model file public static function boot() { parent::boot(); if (...
Cahya Purnama's user avatar
0 votes
1 answer
68 views

Why some of my laravel queue jobs are executing twice and update balances in database twice?

I am using laravel 8 for my application and every is working fine except sometimes my queue jobs are running twice which is causing database ledger balance to update twice . I found out this issue ...
Ashu's user avatar
  • 1
0 votes
0 answers
24 views

How can I create a hasManyThrough relationship using a pivot table?

I realize this question has been asked many times throughout SO. What I'm finding is that answers/suggestions are for much older versions of Laravel, or are using raw SQL to create the join(s). I am ...
Damon's user avatar
  • 4,183
0 votes
1 answer
72 views

How best to optimise a large where in query?

This is a Laravel eloquent query. The goal is to find all the photos that have the person_id of 1 whilst not having a face with a person_id of 1. Archive of an individuals life not a nefarious big ...
BobB's user avatar
  • 752
0 votes
0 answers
23 views

How to Filter Relationships with Null 'Type' and Polymorphic Many-to-Many in Filament's ComponentRelationManager

I have a challenging question for today. I have these two relationships. In Filament's ComponentRelationManager, I have the "attach" action. However, before becoming relationships, the sub-...
Davide Cavallini's user avatar
0 votes
0 answers
38 views

Can't Seem To Fetch The Authenticated User In Laravel In My Helper Class

I am a laravel application however i am unable to fetch the logged in user. I happen to log in successfully. The logout functionality works but when i try fetch the authenticated user using auth()->...
James Donohue's user avatar
0 votes
0 answers
17 views

Laravel 10 updating child records with dynamic input fields

I want to update child records but it's updating the records with last element in an array. Records to update: array:2 [ // app\Http\Controllers\VariantController.php:134 "name" => &...
Usama's user avatar
  • 77
0 votes
0 answers
50 views

Allowing user create a category table advisable

I’m developing an inventory system with laravel,users can create categories and get a form dynamically to populate those categories,but I’m having an issue with querying the result into a responsive ...
Damsonspecial 's user avatar
0 votes
1 answer
22 views

Eloquent Many-To-Many on same model, get relation both ways on loading

I have Report A, Report B and Report C. Reports B and C are related to Report A. When I'm loading a Report A, it shows related reports Report B and C. But then when I load Report B or Report C, the ...
Kristjan O.'s user avatar
0 votes
1 answer
35 views

fetch big number of data in blade and pdf print in laravel

i need to fetch around 30k records from the database to be shown ALL in the blade and in PDF print i have tried the following method but each one has a problem 1- I have used paginate() it works fine ...
Shady Hesham's user avatar
1 vote
1 answer
30 views

Laravel scope comparing two related model attributes

In my Laravel application, there's a "Transaction" model, a "Concept" model and an "Account" model. Each $transaction belongs to both a $concept and an $origin_account. I ...
E. Barney's user avatar
  • 383
0 votes
0 answers
56 views

I was unable to achieve the desired result with just one query

I'm using Laravel Eloquent to grab some videos in a certain order. Is it possible to achieve the same result with just one query? Random order is not necessary. Maybe retrieve the videos in the ...
Jefferson Moreira's user avatar
0 votes
0 answers
34 views

How to "Show information about an Eloquent model" that is not in App\Models namespace

Most of the time i need to see what in which model, i do the following: Open the model file and see relationships Open the database and see what fields it have But now I use the artisan command php ...
shah sawood's user avatar
0 votes
2 answers
53 views

How do i write this second relationship to the same model in Laravel

I have two models, User and Community. A user can belong to many communities while a community can have many users. I have implemented many-to-many relationship for this in both models and then ...
Flexi's user avatar
  • 197
0 votes
1 answer
42 views

Laravel Eloquent MySQL DateTime field update with Interval gives an error

I'm trying to update DateTime fields for all records by adding 1 day using interval with eloquent like that TestModel::update(['date' => \DB::raw('DATE_ADD(date, INTERVAL 1 DAY)')]); But I'm ...
dgpro's user avatar
  • 347
0 votes
1 answer
17 views

Laravel DB: looking for duplicates. Optimize SQL or use Query Builder properly?

I have this code, which works correctly: DB::delete(DB::raw( "delete from my_items where code in (select code from my_items where import_jobs_id = :...
Palantir's user avatar
  • 23.9k
1 vote
1 answer
31 views

Call to undefined relationship [title] on model [App\Models\Employee]

working with Laravel 9 and in My App there are Relationship one to many with Employee Model between both Title and Salary. Employee Model public function titles(): HasMany { return $this-&...
amaruwakoi's user avatar
1 vote
1 answer
42 views

Laravel $model->increment() affects all rows, not just one

Trying to track page views using increment() except it affects every row instead of just the one public function getPost($id) { $post = Post::find($id); $post->increment('views'); ...
kryz's user avatar
  • 89
-1 votes
3 answers
34 views

Get original Laravel Eloquent model on update

I have the following code where I fire an event comparing two versions of the same model. I worked around it by manually setting a "dirty" attribute. However, is there a way to fetch the ...
Denis's user avatar
  • 319
-1 votes
0 answers
14 views

Laravel Eloquent Cascade Delete Not Working for Subcategories When Deleting Main Categories

I have four tables: the first is the 'main' table, the second is the 'super' table, the third is the 'sub' table, and the fourth is the 'super-sub-pivot' table. There are relationships established ...
sonu kumar singh's user avatar
-1 votes
1 answer
37 views

How to select distinct records in eloquent

I'm trying to select unique records using distinct but I am getting repeated records as I have a value in one field that differs. Is there a way to specify to exclude this field or can I use distinct ...
panthro's user avatar
  • 22.9k
0 votes
0 answers
30 views

Laravel, Eloquent upsert method Integrity constraint violation: 1062 Duplicate entry

I'm trying to do a bulk update using the upsert method. But I get a duplication error. Is there anyway I can make this method accept a composite unique key? I'm using mariadb as a database. bd ...
volnistii11's user avatar
0 votes
0 answers
64 views

Get pivot table relationship without N+1 problem [closed]

I have following 4 tables in my Laravel project. student (id, name) activity (id, name) activity_student (id, student_id, activitiy_id) student_activity_media (id, student_activity_id, media) student ...
M Hussain's user avatar
  • 389
1 vote
1 answer
25 views

Laravel Order by Pivot Table Timestamp, Many to Many Relation

I am currently ordering by the timestamp of a model (the Url model) but I want to order by the timestamp of the pivot table instead. Tried a few things but no dice. Can someone help see where I'm ...
Les's user avatar
  • 31
0 votes
0 answers
35 views

How to get code completion for models in laravel

I'm learning laravel using VSCode. I have a problem when I write "Model::" or "Model::query()->" methods like "count" and "insert" are not in the code ...
First Guardian's user avatar
0 votes
1 answer
31 views

Get all conversations by user and display in inbox view in Laravel

I'm creating a basic user to user messaging for my site to allow registered users to private message each other. Now, I'm trying to display all the conversations that I (current logged in user) have ...
0x9342812j1o's user avatar
0 votes
5 answers
66 views

How to add attribute to Eloquent collection?

In Message.php model: public function getAudioUrlAttribute() { return \Storage::disk('public')->url('audio/' . $this->audio); } in MessageController.php $messages = ...
Allen_Tsang's user avatar
0 votes
1 answer
26 views

Messaging with Laravel: Get one common field value of different rows by searching only one or two keywords

I'm trying to create a messaging feature for my Laravel website to allow users to message each other. Here's my tables so far. Table: participants id thread_id user_id 1 8 12 2 8 24 Table: threads ...
0x9342812j1o's user avatar
0 votes
1 answer
36 views

Laravel "Spatie permission" roles sorting with API Resource

I am using Vue 3 with Laravel API resource. With Spatie permission package I am only getting "user" & "editor" along with their roles API (with formatting - see below). The ...
Tauhid's user avatar
  • 37
-1 votes
1 answer
57 views

Laravel 10 relation dynamic variable in subquery

How can I call a dynamic variable in a subquery? I have some relations and call them like: $locations = Location::query() ->with('brands', function ($query) { $query->with('employees'...
vriic's user avatar
  • 19
0 votes
0 answers
25 views

How to integrate in laravel filament form the model connected through hasOneThrough relationship

I have ClientResource and wants to add in the form the fields from the model Status. The structure of model draws like this: Client -> Category -> Status I can add Category fields through ...
makoy's user avatar
  • 1
1 vote
1 answer
37 views

Laravel 3 way pivot table with array

Hello I have these columns participant_id, choice_id and question_id in participant_choice_question table. What I want to achieve is to attach also the question_id to a table row. participant_id|...
Ryan R's user avatar
  • 23
0 votes
2 answers
29 views

How to retrieve data Jumlah Hadir, Jumlah Sakit, Jumlah Izin based on a user in Laravel

I have tried everything to get the data total of Jumlah Hadir, Jumlah Sakit, and Jumlah Izin there for every user. as a beginner I am very confused. For a clear view I want, you can see the image ...
Muhammad Robi Rahman's user avatar
0 votes
3 answers
49 views

Laravel join in HasMany relationship

I have three models: EmailTemplate EmailSend Attendee An EmailSend references both an Attendee and an EmailTemplate: class EmailSend extends MyBaseModel { ... public function attendee() { ...
BattleMetalChris's user avatar
0 votes
1 answer
23 views

Strange result/SQL output from Laravel Eloquent using relationships

I have a user model as follows: use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; class User extends Model { protected $table = 'user'; protected $...
dave sanchez's user avatar
0 votes
2 answers
50 views

laravel search in other table on json column by relation

i try make code for search and this is my code: $services = Service::query()->with('plans')->latest(); if ($request->service_name) { $services = $services->whereRaw(&...
luayjal's user avatar
  • 21
0 votes
2 answers
22 views

Trying to get the propery of object but it is returning unexpected error in laravel?

I have create a laravel object as per the documentation. Though when i am trying to get the value from it, it is throwing the error. Here is a sample code $user = USER::findOrFail(2990); print_r($user)...
A K Sharma's user avatar
0 votes
0 answers
26 views

In Laravel, is there a way to syncWithoutDetaching and also update pivot values?

According to the API documentation: syncWithPivotValues: sync the intermediate tables with a list of IDs or collection of models with the given pivot values. syncWithouDetaching: sync the intermediate ...
9uifranco's user avatar
0 votes
1 answer
33 views

Laravel Eloquent check if json multidimensional array contains certain index and value

I hope someone can help me with the following. I have a simple table/model named 'activities' with an column called 'attributes', here I store an json array with some slugs. Each attribute can also ...
Adriaan's user avatar
  • 378
0 votes
0 answers
10 views

I'm Facing Truble in Fetching Image in Laravel Nova for Products Resource

I am using Image Field in Laravel Nova "use Laravel\Nova\Fields\Image;" and Hare is the inside of field function "Image::make('Product Image', 'image')->help('Upload a Product image')...
Mani-Peer's user avatar
0 votes
1 answer
35 views

Specific query with "where" and "whereHas(Relation)"

Code looks like this: $query = Course::where('author_id', Auth::user()->id)->where( 'name', 'ilike', '%' . $this->searchForm . '%' )->orWhere( ...
Дмитрий Колосков's user avatar
-2 votes
0 answers
39 views

In a Laravel Eloquent query, find() causes all records to be selected and my memory to run out, but where() works

$foo = Foo::query()->find($fooID) // works fine $bar = Bar::query()->find($foo->baz_id) // out of memory I dd($bar->toSql()) and it just did a "select * from bar where is_deleted is ...
figgyfarts's user avatar
0 votes
0 answers
34 views

use policy as condition on query builder laravel

I have this code $userSpacesTimelines = $user ->spaces() ->select('spaces.id', 'spaces.uuid', 'spaces.slug', 'spaces.privacy_type') ->with('...
Hendri Triwanto's user avatar
0 votes
1 answer
37 views

Laravel saving question_id from question table in choices table with belongsTo relation

I am doing question and answer form. I have 2 tables questions table and choices table. I am perfectly saving the question data and choices data to their table except the question_id in choices table. ...
Ryan R's user avatar
  • 23
-2 votes
0 answers
29 views

Laravel Breeze on Windows 10, Docker, and WSL2, QueryException error when saving a new user

I have successfully setup a new Laravel project on Windows 10 using Docker, WSL2, and MySQL. I have setup the project via Laravel build service. Once I got Laravel up and running I have installed ...
mikhail-t's user avatar
  • 4,109
1 vote
1 answer
33 views

Laravel one-to-Many and belongsTo Relationship

I have Order Table and Booking Table A relation between Order Table and Booking Table is saved in 'order_bookings` Table I want to fetch Order from the Booking Table My Booking Model class Booking ...
B L Praveen's user avatar
  • 1,850
1 vote
1 answer
33 views

Laravel: limit many-to-many eager loading by pivot table data

I'd like to limit a many-to-many relation loaded by Laravel eager loading by data located in the pivot table for the many-to-many relation. table_a table_b table_pivot (connects table_a and table_b ...
drakanor's user avatar
  • 119
1 vote
1 answer
45 views

Eloquent : How to add the "when" condition in a "with" method?

I am using Laravel 10. I have this query, with several "with" methods : return Result::where('school_id', $school->id) ->where('user_id', $user->id) ->with(['work'...
Dom's user avatar
  • 3,056
0 votes
2 answers
39 views

How to pass attributes in Laravel scope using name argument (PHP8 feature)

In my Setting model. I have a local scope that accepts $data array argument. The $data contains two keys group and property and any of them can be null as you can see in my query. public function ...
M-Khawar's user avatar
  • 855

1
2 3 4 5
540