I got table called Vehicles
in whose model Vehicle
are defined two relationships.
public function type(){
return $this->belongsTo("App\VehiclePivot", 'vehicle_type_id', "id");
}
and
public function workOrganization(){
return $this->belongsTo("App\WorkOrganization", 'workOrganization_id', "id");
}
When used as
$vehicles = Vehicle::with("type", "workOrganization");
they work great. Show for every vehicle their type and their work organization.
If i try
$vehicles = Vehicle::whereHas("type", function($query) use($request){
$query->where('name', '=', $request->type);
})->with("type")->get();
or
$vehicles = Vehicle::whereHas("workOrganization", function($query) use($request){
$query->where('name', '=', $request->workOrg);
})->with("workOrganization")->get();
They also work.
User can enter type or workOrganization, or both or neither of them.
If one is present, then only that relationship should "execute", if both, then both relationship should, also if it neither of them, then also both should execute.
Can it be done via eloquent?