calling a controller when pressing button

时间:2019-01-07 13:34:55

标签: php laravel eloquent

I am trying to add a new button which is for returning a used card. I am not sure why the data is not being sent with the request. here is my index.blade.php

<form action="{{ route('assignees.destroy',$assignee->id) }}" method="POST">
                    <a class="btn btn-info" href="{{ route('assignees.show',$assignee->id) }}">Show</a>
                    <a class="btn btn-primary" href="{{ route('assignees.edit',$assignee->id) }}">Edit</a>
                    <a class="btn btn-info" href="{{ route('returncard',$assignee->id) }}">Return Card</a>
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>

and here is my controller where I am defining the return card route:

public function returncard(assignee $assignee)
          {
              //
              $assignee->timeout = now();
              $assignee->update();

                return redirect()->route('assignees.index')
                                ->with('success','assignee updated successfully');
          }

web.php:

Route::resource('assignees','AssigneeController');
Route::get('autocomplete', 'AssigneeController@autocomplete')->name('autocomplete');
Route::get('searchcard', 'AssigneeController@searchcard')->name('searchcard');
Route::get('returncard', 'AssigneeController@returncard')->name('returncard');

output of dd($assignee)

Assignee {#266 ▼
  #fillable: array:9 [▼
    0 => "cabinet"
    1 => "custidno"
    2 => "timein"
    3 => "timeout"
    4 => "refnumber"
    5 => "cardno"
    6 => "refnumber"
    7 => "entrytype"
    8 => "notes"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

when pressing any button nothing is happening and the timeout is not being updated.

2 个答案:

答案 0 :(得分:0)

如果您试图将受让人ID传递给控制器​​方法,则需要在路由中添加一个参数:

Route::get('returncard/{assignee}', 'AssigneeController@returncard')->name('returncard');

资源路由为您处理,特定路由(GET,POST等)则不行。如果使用的是路由模型绑定,则需要确保此参数与方法的参数名称匹配。

如果您按照Indra在评论中的建议在框架中工作,我建议您阅读Laravel文档并遵循概述的命名约定。

答案 1 :(得分:-1)

Route::get('assignees/autocomplete/{assignees}', 'AssigneeController@autocomplete')->name('autocomplete');
Route::get('assignees/searchcard/{assignees}', 'AssigneeController@searchcard')->name('searchcard');
Route::get('assignees/returncard/{assignees}', 'AssigneeController@returncard')->name('returncard');

Route::resource('assignees','AssigneeController');

像这样更改路线,它将起作用。

如果要在资源中创建自己的功能,则必须在资源路由之前使用该功能的路由