0
votes

i have postqs table and reply table. postqs_id is foriegn key in reply table. why im i getting this error?and how to solve it

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 (kk, , 2017-10-27 13:54:03, 2017-10-27 13:54:03))

Controller:

 public function store(Request $request)
 {
   $reply = new Reply_qs();
   $reply->reply = $request->get('reply');
   $reply->postqs_id = $request->get('postqs_id');
   $postqs=$reply->postqs()->get();
   $reply->save();

    return redirect()->route('postqs.show')
                    ->with('success','Your question created successfully');
 }

postqs Model:

 public function reply_qs(){
    return $this->hasMany('App\Reply_qs');
   }

reply_qs Model:

 public function postqs(){
 return $this->belongsTo('App\Postqs', 'postqs_id', 'id');
    }

migration:

  Schema::create('reply_qs', function (Blueprint $table) {

        $table->increments('id')->unique();
        $table->text('reply');
        $table->timestamps('date');

       $table->integer('postqs_id')->unsigned();
       $table->foreign('postqs_id')->references('id')->on('postqs') -
      >onDelete('cascade')->onUpdate('cascade');
    });

blade file:

   <form action="{{ route('postqs.store') }}" method="POST" class="form-
     horizontal">
   {{ csrf_field() }}

         <div class="form-group">
           <label class="control-label col-sm-2" >Reply:</label>
                <div class="col-sm-7">
                  <textarea name="reply" id="reply" class="form-control"></textarea>
              </div>
           </div>
   <input type="submit" class="btn btn-primary" value="Submit" >
2
The error says that this line $request->get('postqs_id'); is returning null.Jackowski
@Jackowski may i know how to solve itkavi
Sure. I can try. Do you have a form? Show the code please.Jackowski
in your form input/select, do you have name="postqs_id"?ljubadr
@Jackowski i edited the code.please have a lookkavi

2 Answers

1
votes

So running off of assumptions until clarified...

This is an example of your Routes:

Route::get('/url/{id}', 'SomeController@view')->name('postqs.show');

Route::post('/reply/{id}', 'Reply_qsController@store')->name('postqs.store');

Your routes will / should look something like this.

Therefore within the controller, you'd do:

public function store(Request $request, $id)
{
    $storeIt = $request->get();

    MODELNAME::create([
        'reply' => $storeIt['reply'],
        'postqs_id' => $id
        //Other entries here if required
    ]);

    return redirect()->route('postqs.show')
        ->with('success', 'Your question has been created');
}

MODELNAME will need to be changed to the model for the relevant table you're intending on storing the information into.

-1
votes

Just Edit Your Form

<form action="{{ route('postqs.store') }}" method="POST" class="form-
   horizontal">
     {{ csrf_field() }}
<input name="postqs_id"  display="hidden" class="form-control" value=
 {{$postqs_id}} />
     <div class="form-group">
       <label class="control-label col-sm-2" >Reply:</label>
            <div class="col-sm-7">
              <textarea name="reply" id="reply" class="form-control"></textarea>
           </div>
        </div>
 <input type="submit" class="btn btn-primary" value="Submit" >