download201401
2017-08-15 21:06
采纳率: 0%
浏览 78
已采纳

Laravel模型不起作用

I'm trying to create post page with comments; however, it's not working when I add the comment to a post because the system doesn't recognize the post id. What I'm I doing wrong that it doesn't know that the post_id is equal to $post_id

I'm getting the following error:

SQLSTATE[HY000]: General error: 1364 Field 'post_id' doesn't have a default value (SQL: insert into comments (body, updated_at, created_at) values (This is a test comment, 2017-08-15 19:51:47, 2017-08-15 19:51:47))

COMMENTS FORM

 <div class="well">
    <h4>Leave a Comment:</h4>

    <form role="form" method="post" action="{{ $post->id }}/comments">
    {{  csrf_field() }}

       <div class="form-group">
          <textarea name="body" class="form-control" rows="3"></textarea>
       </div>

       <button type="submit" class="btn btn-primary">Submit</button>
    </form>
 </div>

ROUTE

Route::post('/posts/{post}/comments', 'CommentController@store');

CONTROLLER

public function store(Post $post)
{
    $post->addComment(request('body'));

    return back();
}

COMMENT MODEL

class Comment extends Model
{
    protected $fillable = ['body'];

    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

POST MODEL

class Post extends Model
{
    public function addComment($body)
    {
        Comment::create([
            'body' => $body,
            'post_id' => $this->id
        ]);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试创建包含评论的帖子页面; 但是,当我将评论添加到帖子时,它不起作用,因为系统无法识别帖子ID。 我错误的是它不知道post_id等于$ post_id

我收到以下错误:

SQLSTATE [HY000]:常规错误:1364字段'post_id'没有默认值(SQL:插入 comments body updated_at created_at )值(这是测试评论,2017-08-15 19:51:47,2017-08-15 19: 51:47))

评论表格

 &lt; div class =“well”&gt; 
  &lt; h4&gt;发表评论:&lt; / h4&gt; 
 
&lt; form role =“form”method =“post”action =“{{$ post-&gt; id}} / comments”&gt; 
  {{csrf_field()}} 
 
&lt; div class =“form-group”&gt; 
&lt; textarea name =“body”class =“form-control”rows =“3”&gt;&lt; /  textarea&gt; 
&lt; / div&gt; 
 
&lt; button type =“submit”class =“btn btn-primary”&gt;提交&lt; / button&gt; 
&lt; / form&gt; 
&lt; / div&gt;  
   
 
 

ROUTE

 <代码>路线::交('/帖子 / {post} / comments','CommentController @ store'); 
   
 
 

CONTROLLER

 公共功能商店 (发布$ post)
 {
 $ post-&gt; addComment(request('body')); 
 
返回back(); 
} 
   
 \  n 

评论模型

  class评论扩展模型
 {
 protected $ fillable = ['body']; 
 
公共函数post()
  {
返回$ this-&gt; belongsTo(Post :: class); 
} 
} 
   
 
 

POST MODEL

  class Post extends Model 
 {
 public function addComment($ body)
 {
 Comment :: create([
'body'=&gt;  $ body,
'post_id'=&gt;  $ this-&gt; id 
]); 
} 
 
公共函数注释()
 {
返回$ this-&gt; hasMany(Comment :: class); 
} 
} 
    
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douwen2072 2017-08-15 21:09
    已采纳

    post_id isn't in fillable array:

    class Comment extends Model
    {
        protected $fillable = ['body', 'post_id']; //<---
        ...
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douou0977 2017-08-15 21:18

    in your table in mysql make sure your post_id is auto increment or can be "null". This error is saying post_id has no value and cant be default.

    From the look of your code post_id is your primary key but not set to auto increment

    评论
    解决 无用
    打赏 举报
  • dsgm5631 2017-08-15 21:34

    I see, your controller function doen't know what post your talking about. You need to use the post id that is coming back from your form and locate the post then you can save the new comment like this

    $comment = new App\Comment(['message' => 'A new comment.']);
    
    $post = App\Post::find(1);
    
    $post->comments()->save($comment);
    

    As shown in the Laravel Docs 5.4 Eloquent it will fill the id in for you.

    Also if I remember correctly you do not need to add ['post_id'] to your array of fillable fields.

    The SQL error can be resolved by using a "column modifier". (see below)

    Schema::table('users', function (Blueprint $table) {
        $table->string('email')->nullable();
    });
    

    Laravel Docs 5.4 Migrations

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题