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 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?
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.
Related
This is my code in blade
<div class="col-lg-6">
<div class="contact-form">
<form id="contact" action="{{url('/reservation')}}" method="post">
#csrf
<div class="row">
<div class="col-lg-12">
<h4>Reservation</h4>
</div>
<div class="col-lg-6 col-sm-12">
<fieldset>
<input name="name" type="text" id="name" placeholder="Your Name*" >
</fieldset>
</div>
<div class="col-lg-6 col-sm-12">
<fieldset>
<input name="email" type="text" id="email" pattern="[^ #]*#[^ #]*" placeholder="Your Email Address" >
</fieldset>
</div>
<div class="col-lg-6 col-sm-12">
<fieldset>
<input name="phone" type="number" id="phone" placeholder="Phone Number*" >
</fieldset>
</div>
<div class="col-md-6 col-sm-12">
<input type="text" name="address" placeholder="Address">
</div>
<div class="col-lg-6">
<div id="filterDate2">
<div class="input-group date" data-date-format="dd/mm/yyyy">
<input name="date" id="date" type="text" class="form-control" placeholder="dd/mm/yyyy">
<div class="input-group-addon" >
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-12">
<input type="time" name="time">
</div>
#foreach($data as $data)
<div class="row">
<div class="col-sm-3">
<p class='mybox text-dark'><input type="checkbox" name="productz[]" value="{{$data->title}}"/>{{$data->title}}</p>
</div>
#endforeach
</div>
<div class=" col-lg-12 mt-5">
<fieldset>
<button type="submit" id="form-submit" class="main-button-icon">Make A Reservation</button>
</fieldset>
</div>
</div>
</form>
</div>
</div>
THIS IS WHAT'S INSIDE MY MODEL
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Reservation extends Model
{
use HasFactory;
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Reservation extends Model
{
use HasFactory;
}
FOR MY CONTROLLER
$reservation = new reservation();
$reservation->productz= implode(", " ,$request->productz);
$reservation->save();
return view('reservation');
}
FROM MY MIGRATED TABLE
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateReservationsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('email')->nullable();
$table->string('phone')->nullable();
$table->string('address')->nullable();
$table->string('date')->nullable();
$table->string('time')->nullable();
$table->string('productz')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('reservations');
}
}
Tried to look for tutorial similar to what I'm doing where I retrieved the data from the database such as title and inserted it to checkbox which is successfully displayed. But I can't insert the data checked into the database.
You should use the with function, which adds a piece of data into your view, in conjunction with the view function.
So something like:
return view('reservation')->with('data', $reservation->productz);
should work. Also do make sure the name of each variable that you're looping over is different from the name of the array itself -> e.g. foreach($datum as $data).
Try to print_r($request->productz) and look the data from checkedbox is printing the correct data or not
The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel 8 error used, what I am doing wrong here?
My form file name -- insertemp.blade.php
<form method="POST" action="/showemployee">
#csrf
<div class="form-group" style="margin-left: 30px">
<label for="id">ID</label>
<input class="form-control" type="number" name="eid" id="empid"><br>
</div>
</form>
My web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('/showemployee',[EmployeeController::class,'show']);
Route::post('/showemployee',[EmployeeController::class,'store']);
My controller file -- EmployeeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;
class EmployeeController extends Controller
{
public function show()
{
$emp = Employee::all();
return view('showemployee',['employees' => $emp]);
}
public function store()
{
echo "I am in store";
error_log(request('eid'));
return redirect('/');
}
}
Route::post('/showemployee', [EmployeeController::class,'store'])->name('form.post');
and
<form method="POST" action="{route('form.post')}}" >
#csrf
<div class="form-group" style="margin-left:30px">
<label for="id">ID</label>
<input class="form-control" type="number" name="eid" id="empid">
</div>
</form>
if still not works
check with :
php artisan route:list
My View:
<form action="/AddJuiceForm" method="POST">
#csrf;
<div class="form-group">
<label for="FruitName">Fruit Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Fruit Name"
name="fruitname">
</div>
<div class="form-group">
<label for="JuiceName">Juice Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Juice Name"
name="juicename">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
My Routes/web.php:
Route::post('AddJuiceForm','JuiceController#insertjuice ');
My Controller:
<?php
namespace App\Http\Controllers;
use App\JuiceModel;
use Illuminate\Http\Request;
class JuiceController extends Controller
{
public function insertjuice()
{
dd(request()->all);
}
}
Result: I am getting 'null' as output
Help me where I am going wrong?
Inject the Illuminate\Http\Request object into the insertjuice method and then call the all() method on the variable within your JuiceController class:
public function insertjuice(Request $request)
{
dd($request->all());
}
Hello im developing a students CRUD in laravel but i have a problem saving the data in my db.
Here is the problem that laravel returns. SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
My store function.
public function store(Request $request)
{
$alumno = Alumno::create();
$alumno->fill($request->all());
$alumno->save();
return redirect('/alumnos');
}
My model:
class Alumno extends Model
{
protected $fillable = ['name','apellido','matricula','correo'];
}
My form:
<form action="/alumnos" method="post">
#csrf
<fieldset class="form-fieldset">
<div class="form-group">
<label class="form-label">Nombre<span class="form-required">*</span></label>
<input type="text" class="form-control" name="name" required/>
</div>
<div class="form-group">
<label class="form-label">Apellido<span class="form-required">*</span></label>
<input type="text" class="form-control" name="apellido" required/>
</div>
<div class="form-group">
<label class="form-label">Matricula<span class="form-required">*</span></label>
<input type="number" class="form-control" required name="matricula" />
</div>
<div class="form-group mb-0">
<label class="form-label">Correo Electronico<span class="form-required">*</span></label>
<input type="email" class="form-control" name="correo" required />
</div>
</fieldset>
<input type="submit" class="btn btn-primary" value="Guardar" />
</form>
What im doing wrong? Please help and thank you!!! :)
The problem is from the form action URL try to change the action to {{ action('YourController#store') }}
You should save data as below:
public function store(Request $request)
{
$alumno = new Alumno();
$alumno = $alumno->create($request->all());
return redirect('/alumnos');
}
and it will work fine.
You should add line to migration
$table->string('name')->nullable();
set form as above
<form action="{{action('YourController#store')}}">
For insert all request inputs
Alumno::create($request->all());
You solve this problem in two different ways
Your database structure has to be changed like below. Default field must be Null. Where description field is your database field.
Like below this
Open your model database/migrations/your_model then use like this code
Schema::table('your_model', function (Blueprint $table) {
$table->string('name')->nullable();
});
I'm trying to insert entries in database table but it's not working. It was working 2 weeks ago but I thing I have accidentally changed something.
my relevant model, migration and controllers are here
Model - Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
//
protected $table='profile';
protected $fillable=['user_id', 'Gender', 'Age', 'Address'];
}
Migration -
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfileTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::create('profile', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->char('Gender', 1);
$table->string('Age')->unique();
$table->string('Address');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Controller - profileController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Profile;
use App\Http\Requests;
use Request;
use Carbon\Carbon;
//use App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
class profileController extends Controller
{
//
public function index(){
return view('pages.profile');
}
public function store(){
$uid=Auth::user()->id;
$input=Request::all();
$input['user_id']=$uid;
$profile = new Profile(array('user_id'=>$uid, 'gender'=>$input['gender'], 'age'=>$input['age'], 'address'=>$input['address']));
return view('welcome');
}
}
The page with form - pages/profile.blade.php
{!! Form::open(array('class' => 'form-horizontal col-xs-10 col-sm-6 col-md-4')) !!}
<!-- <form class="form-horizontal col-xs-10 col-sm-6 col-md-4 ">-->
<fieldset>
<!-- Form Name -->
<legend>User Profile</legend>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-md-4 control-label" for="gender">Gender</label>
<div class="col-md-4">
<div class="radio">
<label for="gender-0">
<input type="radio" name="gender" id="gender-0" value="1" checked="checked">
Male
</label>
</div>
<div class="radio">
<label for="gender-1">
<input type="radio" name="gender" id="gender-1" value="2">
Female
</label>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="age">Age</label>
<div class="col-md-2">
<input id="age" name="age" type="text" placeholder="Age" class="form-control input-md">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="address">Address</label>
<div class="col-md-8">
<textarea class="form-control" id="address" name="address">Address</textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
<!--</form>-->
{!! Form::close() !!}
Also, just in case, my route.php -
Route::get('profile', 'profileController#index');
Route::post('profile', 'profileController#store');
In your migration, you use uppercase column names (i.e. "Gender"), whereas you use lowercase ("gender") when you try to fill your model. Also, you never save your model:
$profile = new Profile(array('user_id'=>$uid, 'Gender'=>$input['gender'], 'Age'=>$input['age'], 'Address'=>$input['address']));
$profile->save(); // <- You need to call save() to persist the model to your database.
return view('welcome');
Under: $profile = new Profile(array('user_id'=>$uid, 'gender'=>$input['gender'], 'age'=>$input['age'], 'address'=>$input['address']));
wrtite : $profile->save();
and the action? of {!! Form::open(array('class' => 'form-horizontal col-xs-10 col-sm-6 col-md-4')) !!}
read this http://laravelcollective.com/docs/5.1/html#form-model-binding