dextersiah1998
1 year ago
563
12
Laravel

How to check which button has been click from controller

Posted 1 year ago by dextersiah1998

I am current using laravel 5.7 I have a form in a bootstrap modal that has 2 tabs and each tab will display its submit button respectively. I am trying to capture which submit button has been click from my controller and proceed to store the value to database based on which button has been clicked.

My button is outside of the form tag is because it is located at the modal's footer hence i use the onclick function to trigger the event.

HTML

<!--Team Tab Button-->
<button action="{{route('project.store')}}" 
    method="POST" 
    class="btn btn-primary" 
    name="create-btn" 
    value="create-team" 
    onclick="event.preventDefault(); document.getElementById('submit-form 
    personal').submit();">Createdd changes
</button>`

<!--Personal Tab Button-->
<button action="{{route('project.store')}}" 
    method="POST" 
    class="btn btn-primary" 
    name="create-btn" 
    value="create-personal" 
    onclick="event.preventDefault(); document.getElementById('submit-form- 
    personal').submit();">Created changes
</button>`

Controller

public function store(Request $request)
{
    $this->validate($request, [
        'p_name' => 'required|min:6|max:50',
        'start_date' => 'required',
        'end_date' => 'required',
    ]);

    $project = new Project;
    $project->p_name = $request->input('p_name');
    $project->start_date = $request->input('start_date');
    $project->end_date = $request->input('end_date');
    $project->colab = $request->input('colab');


    if($request->submit == 'create-team'){

        $project->type = 'team  ';
        $project->save();
    }else{
        return 123; //Keeps returning 123
    }     
}

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