【问题标题】:Comment on posts评论帖子
【发布时间】:2018-03-20 21:24:33
【问题描述】:

大家好,我正在尝试为我网站中的每项服务存储 cmets

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

SQLSTATE[23000]:违反完整性约束:1048 列“services_id”不能为空(SQL:插入comments(body、user_id、services_id、updated_at、@987654326 @) 值 (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>

【问题讨论】:

  • 你提供了一个空值:ggg, 1, , 所以$comment-&gt;services()-&gt;associate($service); 必须解析为空
  • 是的,我知道它来自它,但我不知道如何纠正它。
  • 我们需要查看创建de View的代码。您可能没有发送$services

标签: php laravel laravel-5


【解决方案1】:

你的答案在这里:

SQLSTATE[23000]:完整性约束违规:1048 列“services_id”不能为空(SQL:插入 cmets(body、user_id、services_id、updated_at、created_at)值(ggg、1、 缺失值 , 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(); }

【讨论】:

  • 制作 dd($request) 并查看 services_id 值
  • 现在看看我的答案 - 更改commentController
  • 出现一个新错误! SQLSTATE [22007]:无效的日期时间格式:1366 不正确的整数值:第 1 行的列 'services_id' 的整数值:'{$services->id}'(SQL:插入到comments(body,user_id,@987654328 @, updated_at, created_at) 值 (fff, 1, {$services->id}, 2018-03-20 21:38:34, 2018-03-20 21:38:34))
  • 看起来你出于某种原因在做$comment-&gt;service_id='{$services-&gt;id}';。变量扩展在单引号中不起作用(仅在双引号中),在这里完全没有必要。删除引号和花括号:$comment-&gt;service_id=$services-&gt;id;
  • 终于成功了,非常感谢你们!问题来自大括号!非常感谢
猜你喜欢
  • 1970-01-01
  • 2022-01-10
  • 2017-07-29
  • 2013-04-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
相关资源
最近更新 更多