Home > Blockchain >  CREATE operation with Eloquent ORM shows 500 internal server error
CREATE operation with Eloquent ORM shows 500 internal server error

Time:08-27

I am trying to make a POST request to add a showroom in the Laravel application. When I try to do it with Showroom model using Eloquent ORM , it shows 500 internal server error. But if I do it with DB query, then it successfully CREATE the showroom. I commented out the db query lines and apply dd debugging and found out table for Showroom Model is null. screenshot of debugging

This is my controller code -

public function store(ShowroomRequest $request)
{

    $showroom = new Showroom([
        "name" => $request->get('name'),
        "address" => $request->get('address'),
        "description" => $request->get('description'),
    ]);
    dd($showroom);
    $ret =  $showroom->save();
  
    // $name = $request->input('name');
    // $address = $request->input('address');
    // $description = $request->input('description');

    // DB::table('showroom')->insert(
    //     ['name' => $name, 'address' => $address, 'description' => $description]
    // );
    return redirect()->route('back.showroom.index')->withSuccess(__('Showroom Added Successfully.'));

}

And this is my model -

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Showroom extends Model
{
    protected $fillable = ['name', 'description', 'address'];

    protected static $ordersModel = 'App\Models\Order';
    protected static $reviewsModel = 'App\Models\Review';

    public function Orders()
    {
        return $this->hasMany(static::$ordersModel, 'showroom_id');
    }

    public function Reviews()
    {
        return $this->hasMany(static::$reviewsModel, 'showroom_id');
    }
}

Finally this is my db structure - database structure

Can anyone help me to find out what went wrong here? Thanks in advance.

CodePudding user response:

in controller can you assign static values instead of request->get, and see if it saves. please let me what happens afterwards.

also assign name of table in model like this, protected $table = 'tablename';

  • Related