mercredi 16 mars 2016

Laravel 5: add data to $request->all()

I've been trying to add the id of the current logged-in user into a field in my DB.

But, nothing is stored. I have tried the following methods:

 public function store(StoreChildRequest $request)
    {
    $user = Auth::user();
    $child = $request->all();
    $child->merge(['user_id' => $user->id]);
    $child = new Patient($child);
    $child->save();
    }

But the StoreChildRequest always returns the user_id is not passed.

Also this didn't work:

    public function store(StoreChildRequest $request)
    {
    $user = Auth::user();
    Input::merge(['user_id' => $user->id]);
    $child = new Patient($request->all());
    $child->save();
    }

The same error.

Even if I try to do so with fixed data, the merge won't happen:

public function store(StoreChildRequest $request)
{
    $user = Auth::user();
    Input::merge(['user_id' => "01"]);
    if($child = new Patient($request->all()))
    {
        $child->save();
        return response()->json(['succes' => 'Child saved.']);
    }
    else
    {
        return response()
        ->json($request->getMessageBag()->toArray());
    }

}

I guess my thinking pattern of how laravel fetches the data,... is wrong.

Thank you for your help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire