Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
laravel中[App\comment]上的质量分配_Laravel_Store - Fatal编程技术网

laravel中[App\comment]上的质量分配

laravel中[App\comment]上的质量分配,laravel,store,Laravel,Store,正在尝试在我的博客中添加评论,因此出现以下新错误: Add [body] to fillable property to allow mass assignment on [App\comment]. 这是thr控制器: public function store (blog $getid) { comment::create([ 'body' =>request('body'), 'blog_id'=> $getid->id

正在尝试在我的博客中添加评论,因此出现以下新错误:

Add [body] to fillable property to allow mass assignment on [App\comment].
这是thr控制器:

public function store  (blog $getid)
{
    comment::create([
        'body' =>request('body'),
        'blog_id'=> $getid->id
    ]);

     return view('blog');
}
以下是表格:

<form method="POST" action="/blog/{{$showme->id}}/store" >
   @csrf
   <label> Commentaire </label> </br>
   <textarea name="body" id="" cols="30" rows="2"></textarea> </br>
   <button type="submit"> Ajouter commentaire</button>
</form>

为了避免填充任何给定的属性,
Laravel
具有质量保护功能。要填充的属性应位于模型的
$filleble
属性中

class Comment {
    $fillable = ['body', 'blog_id'];
}
奖金

为了符合标准。你不应该用小写字母命名你的类,它应该是
Blog
Comment
,在
PHP
code和文件名中

不应填充Id,而应将其关联,以便将其正确加载到模型上。因此,假设您的
评论
模型具有博客关系

class Comment {
    public function blog() {
        return $this->belongsTo(Blog::class);
    }
}
您应该分配它。在这里,您可以通过使用模型绑定获得Blog,因此您应该将参数命名为$Blog,以便绑定能够工作。另外,使用请求依赖注入也是一种很好的方法

use Illuminate\Http\Request;

public function store(Request $request, Blog $blog) {
    $comment = new Comment(['body' => $request->input('body')]);
    $comment->blog()->associate($blog);
    $comment->save();
}

大量与奖金相关的清理,不必掌握所有内容,但它是指向我期望大多数Laravel开发人员在某个时候结束的方向的指针。只需将
'body'
添加到
应用程序\comment
模型中的
$filleble
数组中!
use Illuminate\Http\Request;

public function store(Request $request, Blog $blog) {
    $comment = new Comment(['body' => $request->input('body')]);
    $comment->blog()->associate($blog);
    $comment->save();
}