Laravel : method is not supported for this route - php

the error is : The GET method is not supported for this route. Supported methods: POST.
although the website works on localhost but on the server doesnt work ?? any solutions
protected function methodNotAllowed(array $others, $method)
{
throw new MethodNotAllowedHttpException(
$others,
sprintf(
'The %s method is not supported for this route. Supported methods: %s.',
$method,
implode(', ', $others)
)
);
}
layout
#extends('install.layout')
#section('content')
<div class="panel panel-default">
<div class="panel-heading text-center">Login Details</div>
<div class="panel-body">
<div class="col-md-12">
#if ($errors->any())
<div class="alert alert-danger alert-dismissible">
×
#foreach ($errors->all() as $error)
<p>{{ $error }}</p>
#endforeach
</div>
#endif
<form action="{{ url('install/store_user') }}" method="post" autocomplete="off">
{{ csrf_field() }}
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" value="{{ old('name') }}" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" value="{{ old('email') }}" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<button type="submit" class="btn btn-install">Next</button>
</form>
</div>
</div>
</div>
#endsection
########################################################################################################################################################
Web Route
<?php
Route::post('install/store_user', 'Install\InstallController#store_user');
?>
installController
public function store_user(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|string|min:6',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$name = $request->name;
$email = $request->email;
$password = Hash::make($request->password);
Installer::createUser($name, $email, $password);
return redirect('install/system_settings');
}

In your form you may give post method but in route you may assign get method. Please cross check it.

most likely it's because of your URL in the form change your form
from
<form action="{{ url('install/store_user') }}" method="post" autocomplete="off">
to
<form action="{{ url('install,store_user') }}" method="post" autocomplete="off">

Related

My textarea on laravel doesn't give any value when update - laravel 8

I need some advice. I make a view for editing a database that contains textarea form. Unfortunately, when I hit the save button it doesn't give any change to the database. What should I do?
Here's the form in infoupdate.blade.view:
<form action="{{ route('updateinfo') }}" method="POST">
#method('PUT')
#csrf
<div class="intro-y box p-5">
<div>
<div class="mt-3"> <label for="by" class="form-label">Oleh</label> <input name="by" id="by" type="text" class="form-control" value="{{ auth()->user()->username }}" readonly></div>
</div>
<div>
<div class="mt-3"> <label for="selectedTime" class="form-label">Waktu Pengumuman</label> <input name="selectedTime" id="selectedTime" type="text" class="form-control" value="{{ $infos->selectedTime }}" readonly></div>
</div>
<div class="mt-3">
<label class="pb-8" for="contentInfo">Isi Pengumuman</label>
<textarea name="contentInfo" id="contentInfo" class="w-full border-2" rows="10" cols="100">{{ $infos->contentInfo }}</textarea>
</div>
<div class="text-right mt-5">
<button type="submit" class="btn btn-primary w-24">Simpan</button>
</div>
</div>
</form>
Also the update function in DBIController.php:
public function update(Request $request){
$request->validate([
'contentInfo' => 'required|min:16'
]);
DB::table('infos')->where('id', $request->id)->update([
'contentInfo' => $request->contentInfo
]);
return redirect()->route('DBI')->with('message','Data Pengumuman Berhasil di Update');
}
and the route for displaying the form and the route for updating database in web.php :
Route::get('/infoeditor/{id}',[DBIController::class, 'edit'])->middleware('admin')->name('infoeditor');
Route::put('/updateinfo',[DBIController::class, 'update'])->middleware('admin')->name('updateinfo');
on your update route, you need to add /{id}
Route::put('/updateinfo/{id}',[DBIController::class, 'update'])->middleware('admin')->name('updateinfo');
Then, in your controller method, you will add $id as parameter
public function update(Request $request, int $id)
{
$request->validate([
'contentInfo' => 'required|min:16'
]);
DB::table('infos')->where('id', $id)->update([
'contentInfo' => $request->contentInfo
]);
return redirect()->route('DBI')->with('message','Data Pengumuman Berhasil di Update');
}
And in your view, you update the action url with $id
<form action="{{ route('updateinfo', ['id' => $infos->id]) }}" method="POST">

Missing required parameters for [Route: employees.update] laravel

i am creating crud in laravel.i ran into the problem with Missing required parameters for [Route: employees.update] [URI: employees/{employee}]. (View: C:\xampp\htdocs\jbs\resources\views\employees\edit.blade.php)what i tried so far i attach below. i added the model view controller below
Edit Blade Page
#extends('employees.layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Employee</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('employees.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('employees.update',$employees->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>StudentName:</strong>
<input type="text" name="name" value="{{ $employees->studname }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Course</strong>
<input type="text" name="name" value="{{ $employees->course }}" class="form-control" placeholder="course">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Fee</strong>
<input type="text" name="name" value="{{ $employees->fee }}" class="form-control" placeholder="fee">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
Controller
public function edit(Employee $employees)
{
return view('employees.edit',compact('employees'));
}
public function update(Request $request, Employee $employees)
{
$request->validate([
'studname' => 'required',
'course' => 'required',
'fee' => 'required',
]);
$employees->update($request->all());
return redirect()->route('employees.index')
->with('success','Employee updated successfully');
}
Model
protected $fillable = [
'studname', 'course','fee',
];
}
routes
Route::resource('employees','App\Http\Controllers\EmployeeController');
Your route parameter is employee not employees. Route parameter names for resource routes are singular by default. The route is actually like this:
employees/{employee}
So in your edit method of your Controller you are using the wrong parameter name so you are getting a new instance of Employee instead of the Implicit Route Model Binding that would inject the instance based on the route parameter, you need to match the typehinted parameter name to the route parameter name:
// {employee}
public function edit(Employee $employee)
{
...
return view(...., ['employee' => $employee]);
}
Now in the view you will have your actual existing Employee instance that you can use to generate the URL to the route instead of an empty Employee that does not have an id which was returning null:
{{ route('employees.update', ['employee' => $employee->id]) }}
// or
{{ route('employees.update', ['employee' => $employee]) }}
The route system can get the correct key from the model instance.
You should pass $employees->id as a hidden input field.
Make your route as
Route::post('employee/update', 'YourController#yourFunction');
and in the edit page the form action should look like
<form action="{{ route('employees.update'}}" method="POST">
make a hidden input field for passing id
<input type="hidden" name="id" value="{{$employees->id}}"></input>
You should pass the route name to the route function like this
route('route name',parameter1)
Example :
Route::post('employee/update/{id}' ,YourController#update)->name('update_employee');
<form action="{{ route('update_employee',$employees->id) }}" method="POST">
#csrf
...

Laravel admin login isn't working

Admin login don't work and don't give me any error.
I have this routes in web.php file:
Auth::routes();
Route::prefix('admin')->group(function () {
Route::get('/login','Auth\AdminLoginController#showLoginForm')->name('admin.login');
Route::post('/login','Auth\AdminLoginController#login')->name('admin.login.submit');
Route::get('/','AdminController#getIndex')->name('admin.dashboard');
Route::get('/logout','Auth\AdminLoginController#logout')->name('admin.logout');
Route::post('/password/email','Auth\AdminForgotPasswordController#sendResetLinkEmail')->name('admin.password.email');
Route::get('/password/reset','Auth\AdminForgotPasswordController#showLinkRequestForm')->name('admin.password.request');
Route::post('/password/reset','Auth\AdminResetPasswordController#reset');
Route::get('/password/reset/{token}','Auth\AdminResetPasswordController#showResetForm')->name('admin.password.reset');
});
And this functions in controller(I only put here which have the problems)
public function showLoginForm()
{
return view('auth.adminlogin');
}
public function login(Request $request)
{
//validate the form data
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
//attempt to log the user in
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
//if successful, then redirect to their intended location
return redirect('/admin');
}
return redirect()->back()->withInput($request->only('email','remember'));
}
And in resources/views/auth/adminlogin.blade.php i have this code:
#extends('backend.public.includes.head')
<body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">ADMIN Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('admin.login.submit') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('admin.password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
This was working days ago.. and now not, i'm looking all but don't find the error. In networking isn't showing anything and with a debug i don't see anything.
I try to reset password and when i reset it (i have a redirect in the function he redirect me good, only not working in normal login).
Errors aren't showed too
EDIT
Migrate file:
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->increments('id');
$table->string('name',50)->unique();
$table->string('email',200)->unique();
$table->string('password');
$table->boolean('public')->default(true);
$table->rememberToken();
$table->timestamps();
});
}
Seed file:
private $arrayAdmins = array(
array(
'name' => 'Lluis',
'email' => 'lluis.puig#correo.biz',
'password' => 'correo1',
'public' => 1
)
);
public function run()
{
self::seedAdmins();
}
public function seedAdmins()
{
DB::table('admins')->delete();
foreach ($this->arrayAdmins as $admin)
{
$a = new Admin;
$a->name = $admin['name'];
$a->email = $admin['email'];
$a->password = $admin['password'];
$a->public = $admin['public'];
$a->save();
}
}
The admin login isn't working if i created with the seed. (So the problem i guess is with the "user created" with the seed.
I try to create one with php artisan tinker and it works.
SOLVED
I check the seed. The problem was de password isn't encrypted!
This line :
$a->password = $admin['password'];
Must be like this:
$a->password = bcrypt($admin['password']);

Laravel Passing Data from View to Controller

I'm using Laravel 5.3. I'm trying to pass some input from the View to a Controller. Here is what I am doing now in the View:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/update/company') }}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
And here is the route:
Route::put('/update/company', [
'as' => 'updateCompany',
'uses' => 'Auth\RegisterController#update'
]);
And here is the Controller:
public function update(Request $request){
$compEmail = $this->companyEmail;
if( ! $compEmail)
{
echo "Email Invalid";
}
$user = User::all()->where("email", $compEmail)->first();
if ( ! $user)
{
echo "Invalid Company";
}
$user->name = $request->input('name');
$user->confirmed = 1;
$user->confirmation_code = null;
$user->save();
}
This is giving me the error:
Creating default object from empty value on the line $user->name =
$request->input('name');
Any tips?
If you haven't find any $user:
if ( ! $user) {
echo "Invalid Company";
}
you're not returning the method so here:
$user->name = $request->input('name');
$user->confirmed = 1;
$user->confirmation_code = null;
You try to access fields on null value from $user empty var.
You should add return here:
if ( ! $user) {
echo "Invalid Company";
return;
}

Laravel 5.2 form validation redirection issue

I am having some issues with the redirect when the form validation fails.
The code that I am using is the following:
// -> use Illuminate\Support\Facades\Validator;
public function subscribe(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|unique:subscriber|email',
]);
if ($validator->fails()) {
return redirect('main')
->withErrors($validator)
->withInput();
}
$email = $request->input('email');
$randomId = $this->generateRandomUserId();
$subscriberSource = $request->input('utm_source');
// ... Save user
}
And this is my form:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Email</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email"
value="{{ $email or old('email') }}">
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-refresh"></i>Subscribe now
</button>
</div>
</div>
</form>
The users should put their email in the email field and then get validated by the above piece of code. The issue is that the user is never redirected back to the main page
You can use:
$this->validate($request, [
'email' => 'required|unique:subscriber|email',
]);
Instead of creating a new validator istance, so Laravel will automatically redirect back with all errors and all inputs to the previous page if validation fails.

Categories