I'm using Crud Traits From My controllers To Save Time How Can I pass query To From Controller To Trait
i tried this code but I get an error Constant expression contains invalid operations
/**
* The entities to eager load on every query.
*
* @var array
*/
protected $withEntities= [
'shipment'=> Shipment::query()->find($request['shipment_id']),
];
So I can use it in traits like this
trait HasCrudActions
{
/**
* Display a listing of the resource.
*
* @return Application|Factory|View
*/
public function index()
{
return view("{$this->viewPath}.index")->with($this->withEntities);
}
}
CodePudding user response:
So you can have a method in the controller
protected function withEntities($request)
{
return [
'shipment' => Shipment::query()->find($request->input('shipment_id')),
];
}
Then in trait you can use the method
trait HasCrudActions
{
/**
* Display a listing of the resource.
*
* @return Application|Factory|View
*/
public function index(Request $request)
{
return view("{$this->viewPath}.index")
->with($this->withEntities($request));
}
}