Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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 App\Http\Controllers;

use App\Models\Realestate;
use Illuminate\Http\Request;

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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading