Skip to content
Advertisement

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>
    <div class="form-group">
      <label for="description">description:</label>
      <input type="description" class="form-control" id="description" placeholder="Enter description" name="description">
    </div>
    <p>image:</p>
    <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>
    <div class="mt-3">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </form>

</div>
<script>
    $(".custom-file-input").on("change", function() {
      var image = $(this).val().split("\").pop();
      $(this).siblings(".custom-file-label").addClass("selected").html(image);
    });
    </script>

store function in controller :

<?php

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;
    $name=$request->file('image')->getClientOriginalName();
    $request->file('image')->storeAs('public/storage',$name);
    $realestate->image=$name;
    $realestate->save();
    return redirect('/list');
}
}

The migration which I had used:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class Realestates extends Migration
{

    public function up()
    {
        Schema::create('realestates', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('description');
            $table->string('image');
            $table->timestamps();
        });
    }

}

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?

Advertisement

Answer

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

edit

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
Advertisement