user3201500
user3201500

Reputation: 1618

Get which form is submitted in Laravel in a controller in Update function

I am new to Laravel, I have 2 forms, from which I want to know which form was submitted on controller.

Here is how my controller looks like.

public function update(Request $request, $id)
{
    //IF movetrash form is submited
    if(//IF movetrash form is submited) {
        $page = Pages::where('id', $id) -> first();
        $page -> active = 0;
        $page -> save();
    }

    return redirect()->route('pages.index');
}

Here is how View looks like:

    @section('content') 
            <!-- BEGIN PAGE CONTENT-->
            <div class="row">
                <div class="col-md-12">

                    <!-- BEGIN EXAMPLE TABLE PORTLET-->
                    <div class="portlet box blue-hoki">
                        <div class="portlet-title">
                            <div class="caption">
                                <i class="fa fa-cogs"></i>All Pages
                            </div>
                            <div class="actions">
                                <a href="javascript:;" class="btn btn-default btn-sm">
                                <i class="fa fa-plus"></i> Add Page</a>
                                <a href="javascript:;" class="btn btn-default btn-sm">
                                <i class="fa fa-trash-o"></i> Move To Trash </a>
                            </div>
                        </div>
                        <div class="portlet-body">
                            <table class="table table-striped table-bordered table-hover" id="sample_3">
                            <thead>
                            <tr>
                                <th class="table-checkbox">
                                    <input type="checkbox" class="group-checkable" data-set="#sample_3 .checkboxes"/>
                                </th>
                                <th>
                                     Page Name
                                </th>
                                <th>
                                     Slug
                                </th>
                                <th>
                                     Publish Date
                                </th>
                                <th>
                                     Status
                                </th>
                                <th>
                                     Action
                                </th>
                            </tr>
                            </thead>

                            <tbody>

                            @foreach($pages as $page)
                            <tr class="odd gradeX">
                                <td>
                                    <input type="checkbox" class="checkboxes" value="{{$page->id}}"/>
                                </td>
                                <td>
                                     {{$page->pagename}}
                                </td>
                                <td>
                                    {{$page->slug}}
                                </td>
                                <td>
                                    {{$page->datepublished}}
                                </td>
                                <td>
                                    @if ($page->status == 1)
                                    <span class="label label-sm label-success">
                                    Published </span>
                                    @else
                                    <span class="label label-sm label-warning">
                                    Draft </span>
                                    @endif
                                </td>
                                <td>
                                    <a href="#"><i class="fa fa-pencil-square-o"></i></a> &nbsp;&nbsp;&nbsp; <a href="#"><i class="fa fa-trash-o"></i></a>
                            {!!Form::open([
                            'method' => 'patch',
                            'route' => ['pages.update', $page->id]
                            ])!!}
                                    <!--<a href="{{route('pages.destroy', $page->pageid)}}"><i class="fa fa-trash-o"></i></a>-->

                                    {!!Form::button('<i class="fa fa-trash-o"></i>', array('class' => 'btn btn-danger', 'type' => 'submit', 'name'=> 'movetrash'))!!}
                                    <!--<button type="submit" name="movetrash" class="btn btn-danger"><i class="fa fa-trash-o"></i></button>-->
                            {{ Form::close() }}
                                </td>
                            </tr>
                            @endforeach


                            </tbody>
                            {!!Form::close()!!}

                            </table>
                        </div>
                    </div>
                    <!-- END EXAMPLE TABLE PORTLET-->
                </div>
            </div>
            <!-- END PAGE CONTENT-->

@stop

Now, how to get that "movetrash" button is submitted?

Any suggestions will be helpful.

For reference here is how my view look like, i have a table to select multiple pages from the checkbox, or i can delete individual pages from the red button in the right.

enter image description here

Output of dd($page)

enter image description here

Upvotes: 0

Views: 828

Answers (2)

smartrahat
smartrahat

Reputation: 5649

First add below line at the top of your file in order to use Input facade.

use Illuminate\Support\Facades\Input;

Update Method's Code:

public function update(Request $request, $id)
{
    //IF movetrash form is submited
    if(Input::has('movetrash')) {
        $page = Pages::where('id', $id) -> first();
        $page -> active = 0;
        $page -> save();
    }

    return redirect()->route('pages.index');
}

Upvotes: 0

mcklayin
mcklayin

Reputation: 1360

You can retrive a submit name button in controller method:

public function update(Request $request, $id) {
    //IF movetrash form is submited
      if($request->has('movetrash')) {
          $page = Pages::where('id', $id) -> first();
          $page -> active = 0;
          $page -> save();
      }

      return redirect()->route('pages.index'); }

Look here in docs

Upvotes: 1

Related Questions