Skip to content

store function laravel 8 not saving data to the database

Thanks in advance for taking the time to read throw the question.

so I have a form in a blade view where a user can add a name, a description, and upload an image but the data is not being passed to the database

The blade view:

<div class="container">

  <form action="/list">
    <div class="form-group">
      <label for="Name">Name:</label>
      <input type="Name" class="form-control" id="Name" placeholder="Enter Name" name="Name">
    <div class="form-group">
      <label for="description">description:</label>
      <input type="description" class="form-control" id="description" placeholder="Enter description" name="description">
    <div class="custom-file mb-3">
      <input type="file" class="custom-file-input" id="image" name="image">
      <label class="custom-file-label" for="image">Choose file</label>
    <div class="mt-3">
      <button type="submit" class="btn btn-primary">Submit</button>

    $(".custom-file-input").on("change", function() {
      var image = $(this).val().split("\").pop();

store function in controller :


namespace AppHttpControllers;

use AppModelsRealestate;
use IlluminateHttpRequest;

class RealestatesController extends Controller
    function store(Request $request){

    $realestate =new Realestate();
    $realestate->name= $request->name;
    $realestate->description= $request->description;
    return redirect('/list');

The migration which I had used:


use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class Realestates extends Migration

    public function up()
        Schema::create('realestates', function (Blueprint $table) {


and at last the route which I’m using is:

Route::post('/list',[RealestatesController::class, 'store']);

did I miss anything in the previously mentioned code?



You’re missing 2 things on your form:

method='POST' — this tells the form to actually submit the form as a POST request and not a GET request

@csrf — All Laravel forms must have a CSRF token to be accepted, unless it is explicitly bypassed in the middleware or submitted via GET, such as a search form


Also for file uploads, you need to add enctype="multipart/form-data" to your form.

User contributions licensed under: CC BY-SA
4 People found this is helpful