0

I have input fields which submit in this manner: dental_plan[property_name] and I can fetch them in controller by doing $request->dental_plan. When I return the request, I get 5 fields which I am sending through. Since DB schema is made so that dental plan has to have patient ID, I need a way to add that property to dental_plan[] but these solutions seem to be failing:

$request['dental_plan']['patient_id'] = $patient->id;
array_push($request->dental_plan, ['patient_id' => $patient->id];

I have tried several other solutions by flipping attributes, getting them as object etc. Usually I get the error Indirect modification of overloaded property Illuminate\Http\Request has no effect. What is the right way to do this?

EDIT:

Is it possible to add one specific and one general field in Laravel model so that you can so something like this:

MyModel::create([['specific_field' => $spec], $request->all()]);

2 Answers 2

2

If you have your relationships set up correctly you can do:

$patient->dentalPlan()->create($request->input('dental_plan'));

If not then you just need to assign the array to a variable before adding your parameter:

$dentalPlan = $request['dental_plan'];
$dentalPlan['patent_id'] = $patient->id;

Then you can create your instance with $dentalPlan

Sign up to request clarification or add additional context in comments.

Comments

1

First, the way you accessing object from $request->property actually a magic method that eventually is a public function__get(){}, which is not allow to manipulate as a normal array, even it looks like an array.

The proper to do this is define an array from your input, adding the new key value you want to that array. Then use it to actually save or do anything you want.

$inputAttrs = $request->dental_plan;
$inputAttrs['patient_id'] = $yourPatientId;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.