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());
}
Related
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
Error while navigating from edit/id to home and other pages. Navingation stucks at edit/id and changes to edit/add or edit/home instead of resto/home . Also while hiting update getting error POST method is not supported
This is my route-web code
Route::get('/', function () {
return view('welcome');
});
Route::get('home','RestoController#index');
Route::get('list','RestoController#list');
Route::post('add','RestoController#add');
Route::view('add','add');
Route::get('delete/{id}','RestoController#delete');
Route::get('edit/{id}','RestoController#edit');
This is my edit code
#extends('layout')
#section('content')
<div class="col-sm-6">
<h1>Edit Restaurant</h1>
<form action="edit" method="post">
#csrf
<div class="form-group">
<label>Name</label>
<input type="hidden" name="id" value="{{$data->id}}">
<input type="text" name="name" class="form-control" value="{{$data->name}}" placeholder="Enter Name">
</div>
<div class="form-group">
<label>Email ID</label>
<input type="text" name="email" class="form-control" value="{{$data->email}}" placeholder="Enter Email ID">
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control" value="{{$data->address}}" placeholder="Enter Address">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
#stop
and This is my Controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Restaurant;
use Session;
class RestoController extends Controller
{
//
public function index()
{
return view('home');
}
public function list()
{
$data= Restaurant::all();
return view('list',["data"=>$data]);
}
function add(Request $req)
{
// return $req->input();
$resto = new Restaurant;
$resto->name=$req->input('name');
$resto->email=$req->input('email');
$resto->address=$req->input('address');
$resto->save();
$req->session()->flash('status','Restaurant added successfully');
return redirect('list');
}
function delete($id, Request $req)
{
Restaurant::find($id)->delete();
Session::flash('status','Restaurant deleted successfully');
return redirect('list');
}
function edit($id)
{
$data= Restaurant::find($id);
return view('edit',["data"=>$data]);
}
function update(Request $req)
{
$resto = Restaurant::find($req->input('id'));
$resto->name=$req->input('name');
$resto->email=$req->input('email');
$resto->address=$req->input('address');
$resto->save();
$req->session()->flash('status','Restaurant updated successfully');
return redirect('list');
}
}
please help me and guide me where i am lacking ? also, while hitting update got error - the POST method is not supported, Thanks
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();
});
Problem with displaying content entered in the form in laravel.
What code to add to display the value entered in the form?
--web.php
Route::get('/show-name', ['uses' => 'NameController#show-name', 'middleware' => 'auth']);
--
NameContoller.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name()
{
return view('show-name');
}
}
--
show-name.blade.php
<?php
print_r($_POST);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Message after using the button - MethodNotAllowedHttpException.
I will be grateful for your help.
You're sending a POST request, not a GET request.
Route::post('/show-name', [
'uses' => 'NameController#show-name',
'middleware' => 'auth'
]);
The documentation tells you how to get all the data
https://laravel.com/docs/5.8/requests#retrieving-input
The documentation tells you have to pass data to a view https://laravel.com/docs/5.8/views#passing-data-to-views
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name(Request $request)
{
$input = $request->all();
return view('show-name')->with('data', $input);
}
}
The documentation tells you how to access data passed into a view https://laravel.com/docs/5.8/blade#displaying-data
#php
echo print_r($data);
#endphp
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Further more, the form won't work without CSFR https://laravel.com/docs/5.8/blade#forms
Inputs are self closing, meaning there is no need for </input>
Further more, there is no need for <?php echo $_SERVER['PHP_SELF']; ?> because with Laravel, you can specifically define the name of the route.
<form method="post" action="/show-name" class="">
Watch some basic tutorials on Laravel because you're going about this all incorrectly.
This is how the registration form of FOSUserBundle looks like:
<form action="/Symfony/web/signup/" method="POST" class="fos_user_registration_register">
<div id="fos_user_registration_form">
<input type="hidden" id="fos_user_registration_form__token" name="fos_user_registration_form[_token]" value="c248f3ef17b082803ae9948c03d137c380f0dc24"/>
<div>
<label for="fos_user_registration_form_username">Username:</label><input type="text" id="fos_user_registration_form_username" name="fos_user_registration_form[username]" required="required" maxlength="255" pattern=".{2,255}"/>
</div>
<div>
<label for="fos_user_registration_form_email">Email:</label><input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]" required="required"/>
</div>
<div>
<label for="fos_user_registration_form_plainPassword_first">Password:</label><input type="password" id="fos_user_registration_form_plainPassword_first" name="fos_user_registration_form[plainPassword][first]" required="required"/>
</div>
<div>
<label for="fos_user_registration_form_plainPassword_second">Verification:</label><input type="password" id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required"/>
</div>
</div>
<div>
<input type="submit" value="Register"/>
</div>
So, as you can see,
<input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]"
MAIN QUESTION: How can I change the id to something like id="email" and also the name to something like name="email"? And it has to work, obviously.
Here you can see: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig {{ form_widget(form) }}, but I can't trace this to where it goes. I also presume the RegistrationFormHandler would have to be edited to support these parameters.
Alter buildForm function in RegistrationFormType class:
# FOSUserBundle/Form/Type/RegistrationFormType.php
class RegistrationController extends ContainerAware
{
// ...
public function getName()
{
return 'fos_user_registration';
}
}
Change fos_user_registration to whatever you want.