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 >
<form action="/list">
<div >
<label for="Name">Name:</label>
<input type="Name" id="Name" placeholder="Enter Name" name="Name">
</div>
<div >
<label for="description">description:</label>
<input type="description" id="description" placeholder="Enter description" name="description">
</div>
<p>image:</p>
<div >
<input type="file" id="image" name="image">
<label for="image">Choose file</label>
</div>
<div >
<button type="submit" >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?
CodePudding user response:
Try changing action="/list" to action="{{url('/list')}}"
CodePudding user response:
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