Venus
Venus

Reputation: 11

Laravel error : Integrity constraint violation

Routes

Route::post('/review','RentalController@review');
Controller
public function review(Request $request)
{
    $review = new Reviews();
    $rpId = rand();
    $review->rvid=$rpId;
    $review->usid_fk = Auth::user()->uid;
    // $propId= $request->input('propId');
    $review->prId_fk = $request->input('propId');
    $review->comment = $request->input('comment');
    $review->rating = $request->input('rating');
    $review->date = Carbon::now();
    $review->save();
}
Migration File
public function up()
{
    Schema::create('review', function (Blueprint $table) {
        $table->integer('rvId')->primary();
        $table->integer('usId_fk');
        $table->foreign('usId_fk')->references('uid')->on('users');
        $table->integer('prId_fk');
        $table->foreign('prId_fk')->references('pId')->on('properties');
        $table->date('date');
        $table->integer('rating');
        $table->string('comment');
    });
}
View (Blade template)
<form action="{{ url('/review') }}" method="POST">
    {{ csrf_field() }}

    <div class="modal-body">
        <input type="hidden" name="propid" value="{{ $prop->propid }}"/>
        <input id="rating" name="rating" class="rating rating-loading" data-show-clear="false" data-min="0" data-max="5" data-step="1" value="0">
        <div class="input-group">
            <div class="input-group-prepend">
                <span class="input-group-text">Comment</span>
            </div>
            <textarea name="comment" class="form-control" aria-label="Comment"></textarea>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
</form>

And the error is integrity constraint violation error.

The prId_fk cannot be null integrity constraint violation

I have been trying to fix this for a couple of days already. I've been trying to rewrite my code over and over again still it didn't work. Your reply would be highly appreciated.

Thank you!

Upvotes: 0

Views: 98

Answers (2)

Sehdev
Sehdev

Reputation: 5662

You are getting this error because as per your migration file your prId_fk did not accept null values and your hidden inputfile name is propid and your accessing it using propId

You can set your prId_fk to accept null in your migration file as below

$table->integer('prId_fk')->nullable();

OR

access it correctly

$request->input('propid');

Upvotes: 0

tamrat
tamrat

Reputation: 1915

You are sending the data as propid from your form but you are trying to access it as propId in your controller. Make sure the cases match.

Either change the input in the form to

<input type="hidden" name="propId" value="{{ $prop->propid }}"/>

or update your controller to refer to the right index.

$review->prId_fk = $request->input('propid');

Upvotes: 1

Related Questions