mardi 20 mars 2018

Comment on posts

Hey Guys I'm trying to store comments for every service in my website

first I created the relationships between user,comment,service then when I try to add a comment I get an error :

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'services_id' cannot be null (SQL: insert into comments (body, user_id, services_id, updated_at, created_at) values (ggg, 1, , 2018-03-20 21:12:17, 2018-03-20 21:12:17))

That's the service model : 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');

}}

That's the model 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');
}}

That's the model 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');
}}

the route:

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

Store method in 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(); }

And that's the blade page

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



                     <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>



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2FOmzFI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire