在Bootstrap模式(Laravel)中对按钮执行特定操作

时间:2018-03-20 07:38:21

标签: php jquery laravel

我创建了一个模式,显示特定于其ID条目的信息,并将批准和拒绝按钮放置如下。

Screenshot of modal

当用户点击“接受”或“拒绝”时,它需要传递与查看的条目相关的ID,以便用户可以执行请求的操作,无论是接受还是拒绝条目(默认状态为“待定”)

vendor.blade.php

@RestController
public class CVIController {
    @RequestMapping(value = "/resume")
    public @ResponseBody ResponseEntity<Integer> getResume() {
        return new ResponseEntity<Integer>(5, HttpStatus.OK);
    }
}

VendorController.php

<div class="modal-footer">
  <span class="pull-left">
    <form method="POST" action="{{ route('approve') }}">
      @method('PUT')
      @csrf
      <button type="submit" class="btn btn-success">Approve</button>
     </form>
  </span>
  <span class="pull-right">
    <form method="POST" action="{{ route('reject') }}">
      @method('PUT')
      @csrf
      <button type="submit" class="btn btn-danger">Reject</button>
    </form>
  </span>
</div>

ApprovedVendorController.php

public function index()
{
    $vendors = DB::select('select company_name, roc_no, created_at from mides_vendors');

    $vendor_id = Vendor::where('status', 'Pending');

    return view('panel.vendor', ['vendors' => $vendors]);
}

路由/ web.php

<?php

namespace App\Http\Controllers;

use App\User;
use App\Vendor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ApproveVendorController extends Controller
{
    public function approve(Request $request, $id)
    {
    	DB::insert('insert into mides_users(name, email, password) select name,roc_no,password from mides_vendors where id = :id', ['id' => $id]);
    	DB::update('update mides_vendors set status = :status where id = :id', ['status' => 'Approved', 'id' => $id]);

    	return redirect('/');
    }

    public function reject(Request $request, $id)
    {
    	DB::update('update mides_vendors set status = :status where id = :id', ['status' => 'Rejected', 'id' => $id]);

    	return redirect('/');
    }
}

但是,它会返回此错误。

Error got after clicking Accept or Reject

如何传递数据的ID?我按照pass the database value to modal popup所示尝试使用第二个答案选项创建模态(除了ajax之外)。我需要为这些创建另一个控制器吗?

编辑:经过@Wreigh解释后,它有效,意味着状态从“待定”变为“接受/拒绝”。但是,当我返回上一页时,Route::prefix('/panel')->group(function () { Route::get('/dashboard', function () { return view('panel.dashboard'); }); /* These routes only display the information/modal Route::get('/approve-vendor', 'VendorController@showNewRegistration'); // return vendor.blade.php Route::get('/vendor-approved', 'VendorController@showApproved'); // return vendor-approve.blade.php Route::get('/vendor-reject', 'VendorController@showRejected'); // return vendor-reject.blade.php /* These route used to perform the specific action */ Route::put('/approve/{id}', 'ApproveVendorController@approve')->name('approve'); Route::put('/reject{id}', 'ApproveVendorController@reject')->name('reject'); });(该页面用于显示待处理列表模式),我收到错误/panel/approve-vendor

2 个答案:

答案 0 :(得分:0)

您可以通过网址提供id,也可以使用帖子值。但是,让我们尝试通过网址作为参数。

将您的路线更新为:

Route::put('/approve/{id}', 'ApproveVendorController@approve')->name('approve');
Route::put('/reject/{id}', 'ApproveVendorController@reject')->name('reject');

然后以你的形式:

<form method="POST" action="{{ route('approve', $vendorId) }}">
<form method="POST" action="{{ route('reject', $vendorId) }}">

如果您想要通过帖子值。

无需更新路线,但需要更新控制器操作签名:

public function approve(Request $request) {
    $id = $request->input('id');
}
public function reject(Request $request) {
    $id = $request->input('id');
}

然后将其插入表单中:

<input type="hidden" name="id" value="{{ $vendorId }}">

为什么会遇到错误?

在您的函数签名中,您需要一个$id参数,在您的路由定义中,您没有这个参数。 Laravel无法像这样神奇地提供它,你必须通过url作为参数提供它。

答案 1 :(得分:0)

由于您的approve路由与此/approve/{id}类似,因此您无法转到panel/approve,因为您未通过身份证明

所以试试这个:

vendor.blade.php (将put和您的ID删除为隐藏输入)

<div class="modal-footer">
  <span class="pull-left">
    <form method="POST" action="{{ route('approve') }}">
    <input type="hidden" name="id" value="{{ $id}}">
      @csrf
      <button type="submit" class="btn btn-success">Approve</button>
     </form>
  </span>
  <span class="pull-right">
    <form method="POST" action="{{ route('reject') }}">
      @csrf
      <input type="hidden" name="id" value="{{ $id}}">
      <button type="submit" class="btn btn-danger">Reject</button>
    </form>
  </span>
</div>

ApprovedVendorController.php (删除所有参数)

<?php

namespace App\Http\Controllers;

use App\User;
use App\Vendor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ApproveVendorController extends Controller
{
    public function approve()
    { 
        $id = request('id');

        DB::insert('insert into mides_users(name, email, password) select name,roc_no,password from mides_vendors where id = :id', ['id' => $id]);
        DB::update('update mides_vendors set status = :status where id = :id', ['status' => 'Approved', 'id' => $id]);

        return redirect('/');
    }

    public function reject()
    {
        $id = request('id');
        DB::update('update mides_vendors set status = :status where id = :id', ['status' => 'Rejected', 'id' => $id]);

        return redirect('/');
    }
}

routes / web.php (删除参数)

Route::prefix('/panel')->group(function () {
    Route::get('/dashboard', function () {
        return view('panel.dashboard');
    });
  /* These routes only display the information/modal
    Route::get('/approve-vendor', 'VendorController@showNewRegistration'); // return vendor.blade.php
    Route::get('/vendor-approved', 'VendorController@showApproved'); // return vendor-approve.blade.php
    Route::get('/vendor-reject', 'VendorController@showRejected'); // return vendor-reject.blade.php
  /* These route used to perform the specific action */
    Route::put('/approve', 'ApproveVendorController@approve')->name('approve');
    Route::put('/reject', 'ApproveVendorController@reject')->name('reject');
});