我正在尝试构建一个后端管理区域以将用户添加到我的应用程序中(我不希望用户注册我希望它们由管理员设置)。
我已经创建了必要的控制器和请求。
如果数据可以接受,则通过POST请求提交给domain.tld / admin / users,并通过UserController@store
存储。
如果未满足验证条件,则会将用户发送回表单(domain.tld / admin / users / create)并显示错误消息,但不填充先前填写的数据字段
我正在使用Route::resource('admin/users')
进行RESTful路由创建和一个干净的路由文件,我发现当对父路由(domain.tld / admin / users)发出HTTP POST请求时Route::resource()
,FormRequest不会填充旧数据。
但是,当对表单所在的创建路径(domain.tld / admin / users / create)发出HTTP POST请求时,验证将无法正确填充旧数据。
在使用Route::resource('admin/users')
而不必在我的观看中使用{{ old('') }}
帮助程序的情况下,是否有任何方法可以使其正常工作?
UsersController (非常基本)
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Requests\Admin\UserCreateRequest;
class UsersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$users = User::all();
return view('admin.users.index', compact('users'));
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$users = User::all();
return view('admin.users.create');
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(UserCreateRequest $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
UserCreateRequest
<?php
namespace App\Http\Requests\Admin;
use App\Http\Requests\Request;
class UserCreateRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|confirmed|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
];
}
}
create.blade.php
@extends('admin._layouts.master')
@section('title', 'Create a user')
@section('content')
<form method="post" action="{{ action('Admin\UsersController@store') }}">
@foreach ($errors->all() as $error)
<p class="alert alert-danger">{{ $error }}</p>
@endforeach
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
{!! csrf_field() !!}
<div class="form-group">
<label for="first_name">First Name</label>
<input class="form-control" id="first_name" placeholder="First Name" type="text" name="first_name">
</div>
<div class="form-group">
<label for="last_name">Last Name</label>
<input class="form-control" id="last_name" placeholder="Last Name" type="text" name="last_name">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" placeholder="Email" type="email" name="email">
</div>
<div class="form-group">
<label for="email_confirmation">Confirm email address</label>
<input class="form-control" id="email_confirmation" placeholder="Email" type="email" name="email_confirmation">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" id="password" placeholder="Password" type="password" name="password">
</div>
<div class="form-group">
<label for="password_confirmation">Confirm Password</label>
<input class="form-control" id="password_confirmation" placeholder="Password" type="password" name="password_confirmation">
</div>
<button class="btn btn-default" type="submit">Submit</button>
</form>
@endsection
非常感谢任何帮助。
答案 0 :(得分:0)
您希望使用班级Input
来获取Laravel中的旧帖子数据。
<input type="text" class="form-control" value="{{Input::old('name')}}" name="name" placeholder="Enter name" required="required" />