how to pass data from blade file to Controller in

2019-09-09 10:02发布

问题:

I need pass input data in my blade file to controller file blade file input fiels

<input type="number" id="cid" name="cid"  />{{ $collaborator->user()->first()->id}}</input>

I need get above data to my controller addPermission function to save it in table

public function addPermission(Request $request, $id,  Permission $permission)
{
     $this->validate($request, [
        'status'   => 'required'
    ]);

    $permission = new Permission;

    $permission->status = $request->input('status');
    $permission->project_id       = $id;
    $permission->save();
    return redirect()->back()->with('info','Permission has been added to your Collaborator successfully');
}

can you give me an idea?

回答1:

<input type="number" id="cid" name="cid"  />{{ $collaborator->user()->first()->id}}</input>

should be

<input type="number" id="cid" name="cid" value="{{ $collaborator->user()->first()->id }}" />

Then you could access it just as you did with status

$permission->collaborator_id = $request->input('cid');

But the correct way would be to use associate (for one-to-many) or attach (many-to-many)

$permission->collaborator()->associate(Collaborator::findOrFail($request->input('cid')));


标签: php laravel-5