skip to Main Content

Laravel Eloquent: Create custom attribute from subquery

Modal: Subscription.php class Subscription extends Model { use HasFactory; public function payments(): HasMany { return $this->hasMany(SubscriptionPayment::class, 'sub_id'); } public function totalDurationDays(): Attribute { return Attribute::make( get: fn() => $this->payments()->sum('duration_days') )->shouldCache(); } } Controller: SubscriptionController.php $subs = SharedServer::from('subscriptions AS ss') ->select(…

VIEW QUESTION

Laravel – Custom Intermediate Table Models – eager loading

Here is my current setup: <?php namespace AppModels; use Exception; use IlluminateDatabaseEloquentModel; use IlluminateDatabaseEloquentRelationsBelongsTo; use IlluminateDatabaseEloquentRelationsBelongsToMany; class Employee extends Model { protected $table = 'employees'; protected $fillable = [ 'first_name', 'last_name', 'gender', 'birthdate', 'nationality_id', 'company_id', ]; public function positions(): BelongsToMany…

VIEW QUESTION

laravel eagerloading with function does not return relation but separateley works

i have the following models <?php namespace AppModelsMastersSystem; class UserActivationCode extends IlluminateDatabaseEloquentModel { public $table = "user_activation_codes"; } <?php namespace AppModels; use AppModelsModel; use ModulesRequestsModelsEmploymentLeaveType; use AppModelsMastersSystemUserActivationCode; class EmployeeEmployment extends IlluminateDatabaseEloquentModel { public $table = "employee_employments"; public function employeeProfileScore() {…

VIEW QUESTION

Where Year In list of years Laravel

What I want to do is something like this : $listOfYears = Invoice::pluck('year') ; // [ 2001, 2002, 2003 ] $products = Product::whereYearIn('purchase_date',$listOfYears)->get(); the field purchase_date is type date so you can't just use whereIn, you need to apply whereYear…

VIEW QUESTION

handle n+1 with hasManyThrough relation – Laravel

my product has relation called values which is like this: public function values() { return $this->hasManyThrough(VariantValue::class, VariantProductOption::class, 'product_id', 'product_option_id'); } everything works fine but when i have a multiple products in a single pages n+1 problem happens: select `variant_sku_values`.*, `variant_sku_product_options`.`product_id`…

VIEW QUESTION
Back To Top
Search