Home > Net >  Adding basic data to database - Laravel 8 ajax
Adding basic data to database - Laravel 8 ajax

Time:05-15

My laravel framework version is on 8.83.12

I am trying to build a form that creates a database entry based on Vehicle data, Make, Model, Version.. etc.

My Model (name:Vehicle)

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Vehicle extends Model
{
    use HasFactory; /**  Name of columns fillable */
    public $timestamps = false;
    protected $table = 'tbl_vehicles';
    protected $fillable = [
        'make',
        'model_name',
        'version',
        'powertrain',
        'fuel',
        'model_year',
        'image',
        'created_at'

    ];
};

My Controller (name:VehiclesController)

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Vehicle;
use App\Http\Controllers;
use Illuminate\Database\Migrations\CreateVehiclesTable;

class VehiclesController extends Controller
{
    public function index()
    {
        return view('index');
    }

    // Handle insert

    public function store(Request $request)
    {
        // print_r($_POST);
        // print_r($_FILES);
        // // }

        $file = $request->file('image');
        $filename = time(). '.' .$file->getClientOriginalExtension();
        $file->storeAs('public/images', $filename);

        // handle insert vehicle ajax request
        $tbl_vehicles = [
            'make' => $request->make,
            'model_name' => $request->model_name,
            'version' => $request->version,
            'powertrain' => $request->powertrain,
            'fuel' => $request->fuel,
            'model_year' => $request->model_year,
            'image' => $filename
        ];

        VehiclesController::create($tbl_vehicles);
        return response()->json(
            [
            'status' => 200
            ]
        );
    }
}

My Routes

Route::get('/', [VehiclesController::class, 'index']);
Route::post('/store', [VehiclesController::class, 'store'])->name('store');

I also get a Internal Server Error 500 on chrome console.

I am unsure as to whether it is down to the database name not being placed somewhere correctly and/or the fact that it may be looking for a plural of a variable.

CodePudding user response:

Try this refactor, your are wrongly saving the model.

Vehicle Model

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Vehicle extends Model
{
    use HasFactory;

    public $timestamps = false;

    protected $table = 'tbl_vehicles';

    protected $fillable = [
        'make',
        'model_name',
        'version',
        'powertrain',
        'fuel',
        'model_year',
        'image',
        // 'created_at' <- Can be removed because $timestamps is `false`

    ];
};

VehicleController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Vehicle;
use App\Http\Controllers;

class VehiclesController extends Controller
{
    public function store(Request $request)
    {
        $file = $request->file('image');
        $filename = time(). '.' .$file->getClientOriginalExtension();
        $file->storeAs('public/images', $filename);

        // To perform an insert use this syntax
        $vehicle = Vehicle::create([
            'make' => $request->make,
            'model_name' => $request->model_name,
            'version' => $request->version,
            'powertrain' => $request->powertrain,
            'fuel' => $request->fuel,
            'model_year' => $request->model_year,
            'image' => $filename
        ]);

        return response()->json($vehicle); // Status 200 comes by default
    }
}

Routes File

// Group endpoints by controller
Route::controller(VehiclesController::class)->group(function () {
    Route::get('/', 'index');
    Route::post('/store', 'store');
});
  • Related