Reputation: 183
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}">
{{ 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>
Upvotes: 2
Views: 58
Reputation: 5896
Your answer is here:
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, MISSING VALUE , 2018-03-20 21:12:17, 2018-03-20 21:12:17))
Your query want to put to database services_id
but it is undefined in yoour request
So debug it:
Make dd($request)
and see the services_id value
Guess CommentController should look like this:
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(); }
Upvotes: 1