我正在尝试在 Laravel 中验证表单。如果我没有输入所需的值,它会将我重定向回表单,但我$errors
每次都保持不变
object(Illuminate\Support\ViewErrorBag)#198 (1) {
["bags":protected]=>
array(0) {
}
}
我的控制器
<?php
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\Category;
use App\Http\Requests\CategoryRequest;
class CategoriesController extends Controller {
public function store(CategoryRequest $request) {
Category::create($request->all());
return redirect('cpl_biovideo/dashboard');
}
}
我的请求
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CategoryRequest 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 [
'name' => 'required|min:4|max:20'
];
}
}
我的模特
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
}
我的观点
<div class="card">
<div class="card-header">
<h2>Add Category</h2>
</div>
<?php
echo "<pre>";
var_dump($errors);
echo "</pre>";
?>
<form action="{{url('cpl_biovideo/save-category')}}" method="post">
<input type="hidden" name="_token" value="{{csrf_token()}}" />
<div class="card-body card-padding">
<div class="form-group">
<div class="fg-line">
<input type="text" class="form-control input-lg" placeholder="Category Name" name="name">
</div>
</div>
<div class="form-group">
<div class="fg-line">
<div class="select">
<select class="form-control" name="parent_id">
<option value="">Parent Category</option>
<?php
foreach ($categories as $category) {
?>
<option value="<?php echo $category->id ?>">
<?php echo $category->name; ?>
</option>
<?php
}
?>
</select>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary btn-sm m-t-10">Submit</button>
</div>
</form>
</div>