路由资源更新和删除无法使用除商店 laravel 8 以外的多个角色

route resource update and delete not working using multiple roles except store laravel 8

我想 delete 我的管理产品使用路由资源。但是当我提交时,它会转到页面 404update 方法相同。但是我的 URL id 显示...

这是 destroy 方法 http://127.0.0.1:8000/admin/1 上的 URL,但商店运行良好。它指向我在数据库中的 URL 和 stored

这是我在 Admin/ProductController 资源上的 store 方法:

public function store(Request $request)
{
    $request['user_id'] = auth()->user()->id;
    Product::create($request->all());

    return redirect()->back();
}

这是我在 Admin/ProductController 资源上的 destroy 方法:

 public function destroy($id)
 {
     Product::where('id', $id->id)->delete();

     return redirect()->back();
 }

这是我的表单操作 store 方法。此表格工作正常:

<form method="post" action="{{route('admin.store')}}">
    <input type="text" name="category" class="form-control">
    <button type="submit" class="btn btn-primary">Save changes</button>
</form>

这是我的表单操作 delete。问题是当我提交时,它转到 404 页面:

<form method="post" action="{{ route('admin.destroy', $product->id) }}">
    <input type="text" name="category" class="form-control">
    <button type="submit" class="btn btn-primary">Save changes</button>
</form>

这是我的路线 useradmin:

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::group(['as' => 'user.', 'prefix' => 'users', 'namespace' => 'App\Http\Controllers\Users'], function() {
    Route::get('/', [App\Http\Controllers\Users\UsersController::class, 'index']);
    Route::resources(['/' => ProductController::class], ['except' => ['index']]);
});

Route::group(['middleware' => 'admin', 'as' => 'admin.', 'prefix' => 'admin', 'namespace' => 'App\Http\Controllers\Admin'], function() {
    Route::get('/', [App\Http\Controllers\Admin\AdminController::class, 'index']);
    Route::resources(['/' => ProductController::class], ['except' => ['index']]);
});

这是我的路线列表:

+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+------------+
| Domain | Method    | URI                    | Name             | Action                                                                 | Middleware |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+------------+
|        | GET|HEAD  | /                      |                  | Closure                                                                | web        |
|        | POST      | admin                  | admin.store      | App\Http\Controllers\Admin\ProductController@store                     | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | admin                  | admin.index      | App\Http\Controllers\Admin\ProductController@index                     | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | admin/create           | admin.create     | App\Http\Controllers\Admin\ProductController@create                    | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | DELETE    | admin/{}               | admin.destroy    | App\Http\Controllers\Admin\ProductController@destroy                   | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | PUT|PATCH | admin/{}               | admin.update     | App\Http\Controllers\Admin\ProductController@update                    | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | admin/{}               | admin.show       | App\Http\Controllers\Admin\ProductController@show                      | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | admin/{}/edit          | admin.edit       | App\Http\Controllers\Admin\ProductController@edit                      | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | api/user               |                  | Closure                                                                | api        |
|        |           |                        |                  |                                                                        | auth:api   |
|        | POST      | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web        |
|        |           |                        |                  |                                                                        | guest      |
|        | GET|HEAD  | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web        |
|        |           |                        |                  |                                                                        | guest      |
|        | POST      | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web        |
|        | GET|HEAD  | password/confirm       | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | POST      | password/confirm       |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | POST      | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web        |
|        | GET|HEAD  | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web        |
|        | POST      | password/reset         | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web        |
|        | GET|HEAD  | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web        |
|        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web        |
|        |           |                        |                  |                                                                        | guest      |
|        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web        |
|        |           |                        |                  |                                                                        | guest      |
|        | GET|HEAD  | users                  | user.            | App\Http\Controllers\Users\UsersController@index                       | web        |
|        | POST      | users                  | user.store       | App\Http\Controllers\Users\ProductController@store                     | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | GET|HEAD  | users/create           | user.create      | App\Http\Controllers\Users\ProductController@create                    | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | DELETE    | users/{}               | user.destroy     | App\Http\Controllers\Users\ProductController@destroy                   | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | GET|HEAD  | users/{}               | user.show        | App\Http\Controllers\Users\ProductController@show                      | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | PUT|PATCH | users/{}               | user.update      | App\Http\Controllers\Users\ProductController@update                    | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | GET|HEAD  | users/{}/edit          | user.edit        | App\Http\Controllers\Users\ProductController@edit                      | web        |
|        |           |                        |                  |                                                                        | auth       |
+--------+-----------+------------------------+------------------+---------------------------------------

这是我的 App\Http\Auth\LoginController:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::HOME;

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function login(Request $request)
    {
        $inputVal = $request->all();

        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if (auth()->attempt(['email' => $inputVal['email'], 'password' => $inputVal['password']])) {
            if (auth()->user()->role == 'admin') {
                return redirect()->route('admin.');
            } else {
                return redirect()->route('user');
            }
        } else {
            return redirect()->route('login')
                ->with('error', 'Email & Password are incorrect.');
        }
    }
}

这是我的 admin middleware:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (auth()->user()->role === 'admin') {
            return $next($request);
        }

        return redirect()->back();
    }
}

我不知道这有什么问题,store 正常,但 updatedestroy 不正常。我猜错误在 routemiddleware.

我认为您的问题是您没有告诉 delete 表单使用 DELETE 操作。由于 HTML,您不能使用 action="delete",所以您必须 伪造它 而 Blade 允许您这样做。

所以试试这个:

<form method="post" action="{{ route('admin.destroy', $product->id) }}">
    @csrf
    @method('DELETE')
    <input type="text" name="category" class="form-control">
    <button type="submit" class="btn btn-primary">Save changes</button>
</form>

有关它的更多信息,请访问 Blade documentation


您的 update 中应应用相同的修复,您应该在更新表单的标签后使用 @method('PUT')

如果您的方法是 post 那么您还必须添加 CSRF

<form method="post" action="{{ route('admin.destroy', $product->id) }}">
    {{ csrf_field() }}
    {{ method_field('delete') }}
    <input type="text" name="category" class="form-control">
    <button type="submit" class="btn btn-primary">Save changes</button>
</form>