创建类别时如何在Laravel中修复此错误

时间:2019-05-13 17:41:05

标签: laravel laravel-5 laravel-5.5

我正在为博客创建一个网站。我添加了一个快速更新的类别页面,但没有返回到索引:category.php,CategoriesController.php。我正在使用Laravel v5.5和OpenServer。

Category.php

namespace App;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = ['title', 'slug',];

    public function user()
    {
        return $this->belongsToMany(
            User::class,
            'id_idcat',
            'gid',
            'idcat'
        );
    }

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

CategoriesController.php

namespace App\Http\Controllers\Admin;

use App\Category;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use View;

class CategoriesController extends Controller
{
    public function index()
    {
        $categories = Category::all();

        return view('admin.categories.index', ['categories' => $categories]);
    }

    public function create()
    {
        return view('admin.categories.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required' //обязательно
        ]);

        Category::create($request->all());

        return redirect()->route('admin.categories.index');
    }
}

create.blade.php

{!! Form::open(['route' => 'categories.store']) !!}
<div class="box-header with-border">
    <h3 class="box-title">Добавляем категорию</h3>
</div>
<div class="box-body">
    <div class="col-md-6">
        <div class="form-group">
            <label for="exampleInputEmail1">Название</label>
            <input type="text" class="form-control" idcat="exampleInputEmail1" name="cat">
        </div>
    </div>
</div>
<!-- /.box-body -->
<div class="box-footer">
    <button class="btn btn-default">Назад</button>
    <button class="btn btn-success pull-right">Добавить</button>
</div>
<!-- /.box-footer-->
{!! Form::close() !!}

我找不到错误。我做了几次类似的任务。

1 个答案:

答案 0 :(得分:0)

您的数据没有得到验证,因为没有名为title的输入,因为它的名称为cat。因此,您的验证器会将您重定向回带有错误的信息,但是您的刀片没有显示错误,因此您看不到它们。

在创建视图中更改字段名称,并添加如下错误:

编辑:还将CSRF添加到表单

create.blade.php

@if ($errors->any())
    <div class="alert alert-danger" role="alert">
        @foreach ($errors->all() as $input_error)
            {{ $input_error }}
        @endforeach
    </div>
@endif

    {!! Form::open(['route' => 'categories.store']) !!}  
    {{ csrf_field() }}
    <div class="box-header with-border">
        <h3 class="box-title">Добавляем категорию</h3>
      </div>
      <div class="box-body">
        <div class="col-md-6">
          <div class="form-group">
            <label for="exampleInputEmail1">Название</label>
            <input type="text" class="form-control" idcat="exampleInputEmail1" placeholder="" name="title">
          </div>
      </div>
    </div>
      <!-- /.box-body -->
      <div class="box-footer">
        <button class="btn btn-default">Назад</button>
        <button class="btn btn-success pull-right">Добавить</button>
      </div>
      <!-- /.box-footer-->
      {!! Form::close() !!}