0

Trying to integrate "if condition" inside model relation. But It doesnt work.

    $customer_index = Customer::where('firm_id', $firm_id)
    ->with(['company', 'tires', 'vehicles'])
    ->withCount(['tires', 'vehicles'])
    ->orderBy('id', 'desc');

my Customer.php model

public function vehicles()
{
    if(isset($this->company->is_fleet)){
        return $this->hasMany('App\Models\CustomerVehicle', 'fleet_id', 'company_id');
    }
    return $this->hasMany('App\Models\CustomerVehicle');
}
TCS
  • 629
  • 3
  • 11
  • 32
  • 1
    you cant do that with eager loading as the relation method is called on a new instance (that has no attributes) – lagbox Nov 27 '20 at 15:34
  • 1
    Does this answer your question? [How to setup conditional relationship on Eloquent](https://stackoverflow.com/questions/43668153/how-to-setup-conditional-relationship-on-eloquent) – miken32 Nov 27 '20 at 15:50

1 Answers1

1

Can't do that sort of conditional with eager loading.

To maintain all benefits of eloquent you should separate the relations

public function company_vehicles()
{
    return $this->hasMany(CustomerVehicle::class, 'fleet_id', 'company_id');
}

public function customer_vehicles()
{
    return $this->hasMany(CustomerVehicle::class);
}

//A scope to tuck away the eager loads
public function scopeVehicles($query)
{
   return $query->with(['company_vehicles', 'customer_vehicles']);
}

You asked in the comment to club the relations in one key vehicles. This is how you can try


User::vehicles()->get()->map(function($user){

    if(!empty($user->customer_vehicles)){
      
      $user->vehicles = $user->customer_vehicles;
      unset($user->customer_vehicles);
      unset($user->company_vehicles);
      
    } else if(!empty($user->company_vehicles)) {
      
      $user->vehicles = $user->company_vehicles;
      unset($user->customer_vehicles);
      unset($user->company_vehicles);
      
    }
  
  return $user;
});
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
  • Thank you. but It gives: Call to undefined method Illuminate\Database\Eloquent\Builder::getRelated() error. – TCS Nov 27 '20 at 15:58
  • For which line does it give the error can you find in the stacktrace. Are you importing the use statement for the CustomerVehicle – Donkarnash Nov 27 '20 at 16:01
  • Call to undefined method Illuminate\Database\Eloquent\Builder::getRelated(); vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:50 – TCS Nov 27 '20 at 16:03
  • What I mean is look in the stacktrace to find which line of your written code the error occurs not the framework components in the stack – Donkarnash Nov 27 '20 at 16:08
  • ok it returns 2 list. customer_vehicles and company_vehicles. isn't there anyway to return them on same key? example: vehicles: [{ bla bla bla}]. now, it gives like customer_vehicles: [{bla bla}], company_vehicles: [{ bla bla}] – TCS Nov 27 '20 at 16:14
  • Can possibly be done by some collection manipulation, but why do you want to do that? I don't think its worth. Another thing I just noticed is that you already have a `company` relation on User so why don't you have a relation between company and vehicle which you can access from the user via company relation? – Donkarnash Nov 27 '20 at 16:17
  • @TCS Have updated the answer to club the relations on one key `vehicles`. Let me know if that's how you wanted – Donkarnash Nov 27 '20 at 16:45
  • @TCS If the answer solves the issue raised in your question pls remember to mark the answer as accepted/upvote for the benefit of subsequent users especially newbies. An accepted answer makes them understand that the issue in question is solved via the answer – Donkarnash Nov 30 '20 at 18:58