【问题标题】:2 button accessing different method in the same controller with the same route using Laravel2个按钮使用Laravel在同一个控制器中使用相同的路由访问不同的方法
【发布时间】:2017-10-12 21:24:21
【问题描述】:

我的刀片上有 2 个按钮。我想用它们更新相同的数据库列,当我单击按钮 1 时,它将列更新为 1,当我单击第二个按钮时,它将列更新为 2。我使用相同的路线来执行此操作,因为我必须将“id”从视图传递给控制器​​。

视图中的按钮:

  {!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
    {!! Form::submit('Accpet', ['class' => 'btn btn-success']) !!}
  {!! Form::close() !!}

  {!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
    {!! Form::submit('Send to Super Admin', ['class' => 'btn btn-success']) !!}
  {!! Form::close() !!}

路线

Route::get('/notification_list/notification_application/{notification_id}', 'AdminController@approveNotification')->name('show.approve_notification_application');

Route::get('/notification_list/notification_application/{notification_id}', 'AdminController@sendNotificationToSuperAdmin')->name('show.approve_notification_application');

控制器

public function approveNotification($id){

        $notification = Notification::find($id);
        $notification->approved = '2';
        $notification->save();

        return redirect()->route('admin.notification_list');
    }


    public function sendNotificationToSuperAdmin($id){

        $notification = Notification::find($id);
        $notification->approved = '1';
        $notification->save();

        return redirect()->route('admin.notification_list');
    }

我不知道该怎么做。当我点击任何按钮时,似乎只有第二条路线有效,这意味着无论我点击哪个按钮,它总是将表格更新为值为 1。

【问题讨论】:

  • 嗨 Mill3r。当一个问题得到解决时,点击勾号就足够了——我们在这里不使用 [已解决] 设备,我之前已经从您的问题中删除了它。谢谢。

标签: php laravel routes parameter-passing laravel-eloquent


【解决方案1】:

那是因为你不能设置两个或多个具有相同 URL 和方法类型的路由。您可以将相同的 URL 与 Route:get('hi')Route::post('hi') 等其他类型一起使用。

回到你的问题,你可以这样做:

按钮

 {!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::hidden('type', 0) !!}
{!! Form::submit('Accpet', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}

{!! Form::open(['method' => 'GET', 'route' => ['show.approve_notification_application', $userdetail->id], 'style'=>'display:inline']) !!}
{!! Form::hidden('type', 1) !!}
{!! Form::submit('Send to Super Admin', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}

控制器

public function approveNotification(Request $request, $id){

    $notification = Notification::find($id);
    $notification->approved = $request->input('type') == 1 ? 1 : 2;
    $notification->save();

    return redirect()->route('admin.notification_list');
}

不要忘记在命名空间之后的文件顶部插入use Illuminate\Http\Request

路线

保留第一个,删除第二个。

【讨论】:

  • 将方法从 get 更改为 post 也在路线中 - 或 - 将 $request->input('type') 更改为 $request->get('type') 我先行
  • 一个题外话。我知道1 ? 1 : 2 是一个 if else 语句。但是你能告诉我如果不是这样怎么读吗?
  • if $request->input('type') === 1 { $notification->approved = 1 } else { $notification->approved = 2 }
【解决方案2】:

问题的原因:

在路由文件中 - 您调用了 2 个具有相同名称的方法 - 这就是它到达第二条路由的原因(第二个名称覆盖第一个);

如何解决?

首先 - 删除其中一条路线。

然后 - 在表单中添加一个隐藏字段,以便稍后知道单击了哪个按钮

之后 - 您需要在控制器中添加一个 IF - 根据 $id 像这样:

if ($yourHiddenField == 1) {
   ... your code here...
} elseif ($yourHiddenField == 2 ) {
   ... your code here ...
}

(你需要先获取隐藏字段的值)

【讨论】:

  • 我的 id 是一样的。它的用户ID。一个按钮用于接受申请,另一个按钮用于发送以供进一步批准。如何使用 if else 语句在 2 个按钮之间进行选择?
  • 您需要在表单中添加隐藏字段 - 在第一个表单中您将输入 value="1" ,在第二个表单中您将设置 value="2" 。我根据这个更新了我的答案
猜你喜欢
  • 2021-11-05
  • 2018-09-10
  • 2013-09-24
  • 2016-03-29
  • 1970-01-01
  • 2016-03-03
  • 2017-04-05
  • 2016-10-03
  • 2022-01-07
相关资源
最近更新 更多