i have reply_qs table and postqs table.Postqs_id is foreign key in reply_qs table.when i tried to save the reply_qs form data in database,its showed this error.
ERROR:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'postqs_id' cannot be null (SQL: insert into
reply_qs(reply,postqs_id,updated_at,created_at) values (dd, , 2017-09-23 02:54:16, 2017-09-23 02:54:16))
how i can solve it? and please explain why im getting this error.
reply_qs model :
protected $table = 'reply_qs';
protected $fillable = ['reply'];
public function postqs(){
return $this->hasMany('App\Postqs', 'postqs_id', 'id');
}
postqs model :
public function reply_qs(){
return $this->hasMany('App\Reply_qs');
}
store function:
public function store(Request $request) {
$reply = new Reply_qs();
$reply->reply = $request->get('reply');
$reply->postqs_id = $request->get('postqs_id');
$reply->save();
migration:
Schema::create('reply_qs', function (Blueprint $table) {
$table->increments('id')->unique();
$table->text('reply');
$table->timestamps('date');
});
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::table('reply_qs',function(Blueprint $table)
{
$table->integer('postqs_id')->unsigned();
$table->foreign('postqs_id')->references('id')->on('postqs') -
>onDelete('cascade')->onUpdate('cascade');
});
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
postqs_id? Or is that NULL? - cwallenpoolenot nullonly in one table. You have to set those the same: both related fields (idfromreply_qsandpostqs_idfromPostqsneed to be of same kind - not null. - Tpojka