评论帖子

时间:2018-03-20 21:24:33

标签: php laravel laravel-5

嘿伙计们我试图为我网站上的每项服务存储评论

首先我创建了用户,评论,服务之间的关系 然后当我尝试添加评论时出现错误:

SQLSTATE [23000]:违反完整性约束:1048列' services_id'不能为空(SQL:插入commentsbodyuser_idservices_idupdated_atcreated_at)值(ggg,1, ,2018-03-20 21:12:17,2018-03-20 21:12:17))

这就是服务模式:Service.php

                 <?php
      namespace App; 
    use Illuminate\Database\Eloquent\Model;
    class Service extends Model
      {

public function user(){

    return $this->belongsTo('App\User');
}

public function comments(){
    return $this->hasMany('App\comment');

}}

这是模型Comment.php

<?php

namespace App;

   use Illuminate\Database\Eloquent\Model;

    class Comment extends Model
       {

public function  user() {
    return $this->belongsTo('App\User');
}


public function  services() {
    return $this->belongsTo('App\Service');
}}

那是模型User.php

               <?php

             namespace App;

             use Illuminate\Notifications\Notifiable;
             use Illuminate\Foundation\Auth\User as Authenticatable;

            class User extends Authenticatable
            {  
               use Notifiable;

        protected $fillable = ['username', 'email','password','tel','ville','description','image','diplomes','langues',  
];


protected $hidden = [
    'password', 'remember_token',
];


public function services(){

    return $this->hasMany('App\Service');
}

public function comments(){
    return $this->hasMany('App\Comment');
}}

路线:

 Route::post('/services/{services_id}','CommentsController@store');

在CommentsController中存储方法:

 public function store(Request $request, $services_id)
{
   $this->validate($request,array(
        'body'=>'required',
    )); 
  $comment=new Comment;
  $service=Service::find($services_id);
  $comment->body=$request->body;
  $comment->user_id=$request->user()->id;
  $comment->services()->associate($service);
  $comment->save();
  $request->session()->flash('notif','success');
  return back(); }

这就是刀片页面

<form class="col s12"  method="post" action="/services/{$services->id}">
{{ csrf_field() }}


                     <div class="row">
    <div class="input-field col s12">
      <i class="material-icons prefix">insert_comment</i>
      <textarea id="textarea1" name="body" class="materialize-textarea"></textarea>
      <label for="textarea1" style="color: black;">Commentaire</label>
    </div>
  </div>     

        <div class="row">

            <div class="col s12 center-align">

                <input type="submit" value="confirmer" class="btn-large purple  hoverable">
            </div>
        </div>
        </form>

1 个答案:

答案 0 :(得分:1)

你的答案在这里:

SQLSTATE [23000]:完整性约束违规:1048列'services_id'不能为空(SQL:插入注释(body,user_id, services_id ,updated_at,created_at)值(ggg,1, 缺少价值,2018-03-20 21:12:2017,201-03-20 21:12:17))

您的查询想要放入数据库services_id,但在request

中未定义

所以调试它:

点击dd($request)并查看 services_id

Guess CommentController应如下所示:

public function store(Request $request, $services_id)
{
   $this->validate($request,array(
        'body'=>'required',
    )); 
  $comment=new Comment;
  $service=Service::find($services_id);
  $comment->body=$request->body;
  $comment->user_id=$request->user()->id;
  $comment->service_id=$services_id; //here I made change!!!
  $comment->save();
  $request->session()->flash('notif','success');
  return back(); }