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 ] ] ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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 = ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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’ ..

Read more

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 ..

Read more

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 ..

Read more