0

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

user3201500
  • 1,357
  • 2
  • 18
  • 37
  • Your view code is incomplete, but from what I can gather you're nesting a form inside another form, which is [**not allowed**](http://stackoverflow.com/a/379622/351330) in HTML. You should rethink your approach. – Bogdan Jan 30 '16 at 21:02
  • Ok, but i have to do this, as there is no alternative way to do this, i am attaching a screenshot where i have a table, from which i can choose multiple pages to delete at the same time, or i have option to delete individual page at a time. – user3201500 Jan 30 '16 at 21:06
  • No you don't have to do it like that and **you shouldn't** because it breaks HTML standards it might not work as you expect it across browsers. The best solution would be an asynchronous approach that sends XHR requests. But if you consider that to be too complicated, you can handle the multiple items delete separately by creating a form around the **Move To Trash** button, instead of the whole table, and when a user checks a table item, just append a hidden input to that form with the ID of the entry. So when you click on **Move To Trash** you'll be sending all selected IDs to be deleted. – Bogdan Jan 30 '16 at 21:17
  • Yeah, ajax requests will be better, you can user if($request->ajax()) to check if request is ajax. – mcklayin Jan 30 '16 at 21:18
  • @Bogdan. Yes got your point and you are absolutely correct. But i just want to solve this to understand, where i am making mistake. I took off the first form tag. Now i have only one for individual page active = 0. It should now update. – user3201500 Jan 30 '16 at 21:23
  • I'm not really sure what you mean. Please post the entire view code that renders that screenshot so I can see what you're doing. – Bogdan Jan 30 '16 at 21:27
  • In the `update` method replace the `return redirect()->route('pages.index');` with `dd($page);` just to see if the attributes have the correct values. Also you forgot to remove the form close statement in the view after the `tbody` closing tag (although that shouldn't influence the result). – Bogdan Jan 30 '16 at 21:39
  • @Bogdan. getting this error "undefined variable page" – user3201500 Jan 30 '16 at 21:45
  • That's probably because you have that `if` condition before that statement that doesn't evaluate to `true`. Remove the condition since you don't need it, as you should be handling the bulk deletes in a separate controller action. – Bogdan Jan 30 '16 at 21:48
  • Ok is there any way i can call one of my method i created in `PageController` with name `movetotrash` (id as a parameter) and on clicking of a link i can route to that method? We can try this method as well. – user3201500 Jan 30 '16 at 21:51
  • You mean that your `update` method looks [like this](http://kopy.io/0uxfx#C9zgE8zjTZ50yq) and the form does a POST request to that method and you get an error that the `$page` variable is undefined? – Bogdan Jan 30 '16 at 21:53
  • actually i commented the whole IF statement thats why i was getting that error. But now its now throughing that. Its working. I am also attaching a output of that in above. – user3201500 Jan 30 '16 at 21:58
  • Ok, so it seems to update. If you [revert to the redirect response](http://kopy.io/jdUOd#udV0qUQlfFYbMd) you should have the individual delete option working properly. – Bogdan Jan 30 '16 at 22:04
  • Delete option was working fine earlier as well. But i wasnt able to check which form is submitted. – user3201500 Jan 30 '16 at 22:06
  • Can you please also help me with this "Ok is there any way i can call one of my method i created in PageController with name movetotrash (id as a parameter) and on clicking of a link i can route to that method? We can try this method as well. " – user3201500 Jan 30 '16 at 22:07

2 Answers2

1

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

mcklayin
  • 1,142
  • 7
  • 14
  • Thanks, but no its not working :( its redirecting me to else statement even i am clicking on "movetrash" button – user3201500 Jan 30 '16 at 21:09
  • It will redirect, code in if statemnets must work fine, this part `return redirect()->route('pages.index'); ` redirected you. Look at database, if your data saved, everything works. – mcklayin Jan 30 '16 at 21:13
  • Instead of just IF statement i use IF and ELSE. Here is how it looks like `if($request->has('movetrash')){ $page = Pages::where('id', $id) -> first(); $page -> active = 0; $page -> save(); } else{ return "no trash"; }`. But its always going to "no trash" – user3201500 Jan 30 '16 at 21:25
0

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');
}
smartrahat
  • 4,455
  • 5
  • 39
  • 62