I am building an employee management system and i want to prevent admin from deleting a department if any of its child departments have users. When i say delete i mean updating a deleted column in my departments table from 0(active) to 1(deleted). This is the code that i have now Models/Department public function users() ..
Category : eloquent–relationship
$data = Product::with(‘items)->whereBetween(‘date’, [‘2000-01-01’, ‘2000-01-31’])->get(); dump($data); output [ 0=>[ “id”=>11 “type”=>”food” “items”=>[ 0 =>[ “id”=>333 “set_id”=>1 “member_price”=> 22 “non_member_price”=> 0 ] 1 =>[ “id”=>444 “set_id”=>2 “member_price”=> 0 “non_member_price”=> 33 ] ] ] 1=>[ “id”=>22 “type”=>”food” “items”=>[ 0 =>[ “id”=>444 “set_id”=>1 “member_price”=> 44 “non_member_price”=> 0 ] 1 =>[ “id”=>555 “set_id”=>2 “member_price”=> 0 “non_member_price”=> 55 ] ] ..
Laravel/Eloquent newbie here. I am implementing a simple board game. Each game has 4 players. The tables structure consists of a Players table and a Games table: SELECT * FROM players; id | name | ——————— 1 | John | 2 | Mary | 3 | Linda | 4 | Alex | 5 | Chris ..

I have implemented eloquent relationship in my code but Laravel unable to read the function that I created to map the eloquent relationship in the model. User Model public function products(){ return $this->hasMany(Product::class,’userid’); } Product Model public function users(){ return $this->belongsTo(User::class); } Product Controller $products = Product::with(‘Users’)->Users()->where(‘users.isActive’,1)->get(); return view(‘product’,compact(‘products’)); I keep getting error from the ..
I have two tables events and event_series events table has utc_offset and event_series has start_date and start_time i want to add or substract utc_offset from start_date and start_time if i use ->with([‘eventseries’=> function($q){ $q->select(DB::raw("event_series.id")) ->where(DB::raw("(IF((events.utc_offset*-1)>0, DATE_ADD(concat(event_series.start_date, ‘ ‘, event_series.start_time),INTERVAL events.utc_offset MINUTE), DATE_SUB(concat(event_series.start_date, ‘ ‘, event_series.start_time),INTERVAL events.utc_offset MINUTE)) >= UTC_TIMESTAMP())")) ->get(); }])->offset($offset) ->limit($limit) ->get(); throws error ..
I have an ‘Admin’ model which has a manyToMany relationship with a ‘Priv’ model. There is a pivot table named ‘admins_privs’ which links the two together, as many Admin’s can have many privileges. What I need to do is to be able to do (in Blade) is to check if the logged in Admin has ..

I have Bank table and transaction table. The relationship is one-to-many, so a bank may have many transactions. Transaction table has the bank foreign key(bank_id). and each transaction has a column ‘type’ which is either credit or debit. I want to show each bank balance based on its transactions. Here are my questions: How to ..
I have this model file:- namespace App; use IlluminateDatabaseEloquentModel; class Observation extends Model { protected $fillable = [‘observation’, ‘recommendation’, ‘priority’, ‘report_asset_id’]; protected $appends = [‘facility’]; public function attachments() { return $this->hasMany(‘AppObservationAttachment’); } public function report_facility() { return $this->belongsTo(‘AppReportFacility’); } public function getFacilityAttribute() { return $this->report_facility()->facility; } } And this is my query code:- $observations = ..
I have stuffs table where will be saved only stuff title. Stuffs table: id title Also I’ve user_stuffs table where each user has n stuff volume. User stuffs table: id stuff_id user_id volume How I can SUM each stuffs volumes and get like this result: { "title": "Stuff Title", "total": 7634 } I tried using ..
I’m using many to many relationship in laravel 8. Have two models: Room and Service. I have attached some services to a specific room and now, when I want to edit this specific room I need it to show me which services are selected and which are not. This is a code I am using: ..
There’s a table for courses. There’s also an exercise table belonging to the courses, one course can have many exercises, some exercises have a deadline. In the view it show the course, last finished exercise next to the exercise with the nearest deadline and the nearest deadline. I get the exercise with the nearest deadline ..
I’m a bit confused how to achieve this relationship : table stages : id_stages 10792 pivot table stages_presentiel_distanciel : id_stage_presentiel 10792 | id_stage_distanciel 10804 How to return the stage id 10804 referenced in the pivot table "stages_presentiel_distanciel" ? dont blame me for the namming of the tables and fields it’s an old database and this ..
I am trying to develop a p2p app in laravel. I have three tables in my database namely users, loans, and installments. Now, users and loans can have multiple installments and each installment will belong to a user and a loan. So how do I define this relationship? Do I need a polymorphic relationship? If ..
This is about these 4 tables with the following primary keys. Courses: CursusNaam varchar Exercises: OpdrachtID int Exercise progress: OpdrachtVoortgangID int Students: LeerlingID int One course can have many exercises (so exercises has the foreign key CursusNaam). The progress of the exercises per student is stored in the exercise progress table (so it has the ..
This is about these 4 tables with the following primary keys. Courses: CursusNaam varchar Exercises: OpdrachtID int Exercise progress: OpdrachtVoortgangID int Students: LeerlingID int One course can have many exercises (so exercises has the foreign key CursusNaam). The progress of the exercises per student is stored in the exercise progress table (so it has the ..
I have a pivot table in my database that combines users and loans. The table is the following. user_id loan_id amount Now I have the following code to insert data into the database $user = User::find(2); $user->loans()->create([ // Some database insertion ]); $user->loans()->attach(2, [‘amount’ => ‘500’]); And I am getting this error… IlluminateDatabaseQueryException: SQLSTATE[HY000]: General ..
I’m new at Laravel and Programming at that. I have a problem joining model with its relationship, here is what my model: class MainClass extends Model { public function first() { return $this->hasMany(First::class); } public function second() { return $this->hasMany(Second::class); } public function third() { return $this->hasMany(Third::class); } } When i try to get MainClass ..
I have a User Model class that uses UserHasTeams trait, and this User class has a relationship method called projects. Now my question is, is it possible that somehow this projects method ignores the trait UserHasTeams because it appends the team_id in the where clause of my eloquent query, which I don’t want. Here is ..
I am trying to allow users to add categories and tags to their post by: first submitting the details of their post, such as a caption, image and url through a form. (This is related to my Post Model) Then through use of that same form, I want users to select a predefined main category ..
I am developing a p2p app with laravel. I have two tables namely users and loans. Firstly, a user can be a lender or borrower. Then users can have multiple loans and multiple loans belong to multiple users. Also, a loan can belong to one borrower and also to multiple lenders. To explain it further, ..
I’m trying the 4.1 new feature "Inline create", but I can’t seem to associate the ids of the items created. Let me explain what I’m doing / what I want: I have "Folders" that have "Chapters" inside (so 1-n relation). My code: CRUD::addField([ //Folder crud ‘name’ => ‘chapters’, ‘type’ => ‘relationship’, ‘label’ => ‘Unidad’, ‘model’ ..
I have the following model file which has a belongsToMany relation with the portal_users table. class Role extends Model { use HasFactory; protected $table = ‘portal_roles’; protected $hidden = array(‘created_at’, ‘updated_at’,’deleted_at’); public function users() { return $this->belongsToMany(User::class, ‘portal_user_roles’); } } I am trying to find details of all the users that fits into a particular ..
I am trying to allow users to create and add category tags to a post when creating that post in the form. I would further want those tags to appear in the profile view and function as filter buttons to show posts according to the tag names they possess. However, in my attempt to achieve ..
I, ve got problem with eloquent relationships. This is my DB https://i.stack.imgur.com/2we4g.jpg https://i.stack.imgur.com/20KeG.jpg I’ve got Santander ID in santander column in partner table and want to use data from those two table like from one This is my Partner.php model: <?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; class Partner extends Model { use HasFactory; protected ..
I have three tables I need to query. Everything works fine so far, I get everything back I want, but as I just want to select the element in the array that holds the min price I want to ->first() on that one: return Product::with([‘price’ => function($query) use ($today) { $query->where(function ($subquery1) { $subquery1-> *applying ..