评论帖子

Atef Rihane

大家好我想在我的网站上存储每项服务的评论

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

SQLSTATE [23000]:完整性约束违规:1048列'services_id'不能为空(SQL:INSERT INTO commentsbodyuser_idservices_idupdated_atcreated_at)的值(GGG,1,...,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>
亚当·科兹洛夫斯基

您的答案在这里:

SQLSTATE [23000]:违反完整性约束:1048列“ services_id”不能为空(SQL:插入注释(body,user_id,services_id,updated_at,created_at)注释值(ggg,1,MISSING VALUE和2018-03-20 21): 12:17,2018-03-20 21:12:17))

您的查询要放入数据库,services_id但您未定义request

所以调试它:

制作dd($request)并查看services_id

猜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(); }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章