2 years ago

Return Instance with relationships after saving it.

Posted 2 years ago by GuntarV

How do I return the saved instance with Relationship. Below is the store method, I would like to return the $savedRequest with cropInputMaterial information from another table. Code below doesn't work.

public function store(Request $request)
        $attributes['applied_at']= convertToCarbonObj($request->applied_at);
        return $savedRequest->with('cropInputMaterial');

Here is the relation ship I created to cropInputMaterial

public function cropInputMaterial()
        return $this->hasMany(Crop::class);

I know that I could do another query after saving like this:

return FarmCrops::with('cropInputMaterial')->get();

But wouldn't that be a little inefficient, since I have the $savedRequest already? Suggestions are greatly appreciated!! I would like to have a clean and efficient code. Thanks.

Please sign in or create an account to participate in this conversation.