方法[validate]不存在。 - Laravel 5.5

时间:2017-09-07 14:56:48

标签: laravel-5

我正在尝试" laravel-modules"和#34; Laravel许可包"。但是当发布帖子时,它有问题'方法[验证]不存在。'。我添加了“使用验证器”;'但没有变化。在某些主题中,我删除"使用Illuminate \ Routing \ Controller;"在PermissionController中,但它有错误

控制器

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

我的PermissionController

namespace Modules\User\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

use Auth;

//Importing laravel-permission models
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

use Session;
class PermissionController extends Controller
{
    // use Validator;
    public function __construct() {
        $this->middleware(['auth', 'isAdmin']); //isAdmin middleware lets only users with a //specific permission permission to access these resources
    }
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        $permissions = Permission::all(); //Get all permissions

        return view('user::permissions/index')->with('permissions', $permissions);
        // return view('user::index');
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        $roles = Role::get(); //Get all roles

        return view('user::permissions/create')->with('roles', $roles);
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {

        $this->validate($request, [
            'name'=>'required|max:40',
        ]);

        $name = $request['name'];
        $permission = new Permission();
        $permission->name = $name;

        $roles = $request['roles'];

        $permission->save();

        if (!empty($request['roles'])) { //If one or more role is selected
            foreach ($roles as $role) {
                $r = Role::where('id', '=', $role)->firstOrFail(); //Match input role to db record

                $permission = Permission::where('name', '=', $name)->first(); //Match input //permission to db record
                $r->givePermissionTo($permission);
            }
        }

        return redirect()->route('permissions.index')
            ->with('flash_message',
             'Permission'. $permission->name.' added!');
    }
}

Route
Route::group(['middleware' => 'web', 'prefix' => 'permissions', 'namespace' => 'Modules\User\Http\Controllers'], function()
{
    Route::get('/', 'PermissionController@index');
    Route::get('/create', 'PermissionController@create');
    Route::post('/', 'PermissionController@store');

    Route::delete('/', ["as" => "permissions.destroy", "uses" => "PermissionController@destroy"]);
});

2 个答案:

答案 0 :(得分:3)

use Validator的{​​{1}}中添加回来。在PermissionController之后添加此权限。您目前已将其添加到错误的位置。

然后将您的use Auth;代码更改为:

$this->validate(...)

答案 1 :(得分:0)

我正在尝试

$this->validate($request, [
     'password' => 'required|confirmed|min:6',
]);

但是在Laravel 5.7中,以下代码对我有用了

$request->validate([
     'password' => 'required|confirmed|min:6',
]);