I am getting data from backend and I want users to verify and update. What I am doing is passing the data from controller and fill them inside a form in view blade. When the user verifies the data and submit them, I pass them to validation in my method inside controller. As soon as the validation start laravel throws error:
\Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException",
"The GET method is not supported for this route. Supported methods:
POST.
Below is one the method in FormsController.php passing the variable that have been collected from database to view forms.oneedit.blade.php
public function editingone(Request $request)
{
$nameinst = $request->nameinst;
$firstnm = $request->firstnm;
$lastnm = $request->lastnm;
$phn = $request->phn;
$myArray['nameinst'] = $nameinst;
$myArray['firstnm'] = $firstnm;
$myArray['lastnm'] = $lastnm;
$myArray['phn'] = $phn;
return view('forms.oneedit', $myArray);
}
Below I receive the data in forms.oneedit.blade.php in view
<form class="w-full max-w-lg border-4 rounded-lg p-2" action="{{ route('updateeditedone.fomrone') }}" method="post">
#csrf
<input class="#error('firstname') border-red-500 #enderror"
id="firstname" name="firstname" type="text" value="{{ old('firstname', $firstnm) }}" required>
#error('firstname')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
</div>
#enderror
......................
<input class="
#error('lastname') border-red-500 #enderror"
id="lastname" name="lastname" type="text" value="{{ old('lastname', $lastnm) }}" required>
#error('lastname')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
</div>
#enderror
......................
<button type="submit">Submit</button> </form>
Below are among the routes in web.php
Route::post('/editone/formone', [FormsController::class,'editingone'])->name('edit.fomrone');
Route::post('/update/edited/formone', [FormsController::class,'updateeditedngone'])->name('updateeditedone.fomrone');
Below is the method that I am trying to validate the values in FormsController.php where the error occurs
public function updateeditedngone(Request $request)
{
$this->validate($request, [
'nameinstitute'=> 'required|max:255',
'firstname' => 'max:255',
'lastname' => 'max:255',
'phone' => 'required|max:255',
]); }
NB: If I remove the validation process inside the controller and just get the value it works, something like below:
$val = $request->nameinstitute;
dd($val);
With the above I correctly get the values before validation, But if I try to validate them first the error is thrown. Thanks in advance.
Update:
I have edited the validation method so as to direct to a certain view as suggested but still the same error
public function updateeditedngone(Request $request)
{
$validator = Validator::make($request->all(),
[ 'nameinstitute'=> 'required|max:255','firstname' => 'max:255',
'lastname' => 'max:255',
'phone' => 'required|max:255']);
if ($validator->fails())
{
Session::flash('error', $validator->messages()->first());
return redirect()->back()->withInput();
}
dd($validator);
return redirect()->route('form.checkbtn');
php artisan view:clear php artisan cache:clear php artisan route:clear
Run it from CMD
It works for me
Related
How to show an old value / query field from database in mysql, and edit value in Laravel. I'm using Laravel 9x and PHP 8x
Controller.php :
public function edit(Business $business)
{
return view('dashboard.bisnis.edit', [
'item' => $business
]);
}
public function update(Request $request, Business $business)
{
$rules = [
'deskripsi' => 'required|max:255',
'pemilik' => 'required|max:255'
];
$validateData = $request->validate($rules);
Business::where('id', $business->id)->update($validateData);
return redirect('/dashboard/bisnis')->with('success', 'Item has been updated !');
}
Blade.php:
#extends('dashboard.index')
#section('container')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Edit Data Bisnis</h1>
</div>
<div class="col-lg-8">
<form method="post" action="/dashboard/bisnis/{{ $item->id }}" class="mb-5" enctype="multipart/form-data">
#method('put')
#csrf
<div class="mb-3">
<label for="deskripsi" class="form-label">Deskripsi</label>
<input type="text" class="form-control #error('deskripsi') is-invalid #enderror" id="deskripsi" name="deskripsi" required autofocus
value="{{ old('deskripsi', $item->deskripsi) }}">
#error('deskripsi')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
<div class="mb-3">
<label for="pemilik" class="form-label">Pemilik</label>
<input type="text" class="form-control #error('pemilik') is-invalid #enderror" id="pemilik" name="pemilik" required autofocus
value="{{ old('pemilik', $item->pemilik) }}">
#error('pemilik')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
<button type="submit" class="btn btn-primary">Simpan Perubahan</button>
</form>
</div>
<script>
const deskripsi = document.querySelector('#deskripsi');
const pemilik = document.querySelector('#pemilik');
</script>
#endsection
Also when navigating through my menu such as Business, the sidebar seems cant to be clicked, nor use. Thank you so much
Please try like this:
{{ old('deskripsi') ? old('deskripsi') :$item->deskripsi }}
Please replace this:
return redirect('/dashboard/bisnis')->with('success', 'Item has been updated !');
to
return redirect()->back()->with('success', 'Item has been updated !');
I assume you use Laravel 9
Referring to Repopulating Forms - Validation
Controller.php:
you should use
$deskripsi = $request->old('deskripsi');
$pemilik = $request->old('pemilik');
before
$validateData = $request->validate($rules);
Blade.php:
you should use this on input
value="{{ old('deskripsi') }}"
value="{{ old('pemilik') }}"
By default old will return null if no input exists so we don't need to use nullcheck like
{{old('deskripsi') ?? ''}}
To repopulate value using old() in Laravel you need to return a response withInput(). Not just response.
The return code should
return redirect('/dashboard/bisnis')->with('success', 'Item has been updated !');
change to this
return redirect('/dashboard/bisnis')->with('success', 'Item has been updated !')->withInput();
The solution is i forgot to pass my $id on my controller and route (web.php). Here's my route
Route::controller(GroupServiceController::class)->middleware('auth')->group(function () {
Route::get('/dashboard/gruplayanan', 'index');
Route::get('/dashboard/gruplayanan/create', 'create')->name('gruplayanan.create');
Route::post('/dashboard/gruplayanan', 'store')->name('gruplayanan.store');
Route::get('/dashboard/gruplayanan/edit/{id}', 'edit')->name('gruplayanan.edit');
Route::post('/dashboard/gruplayanan/update/{id}', 'update')->name('gruplayanan.update');
Route::post('/dashboard/gruplayanan/delete/{id}', 'destroy')->name('gruplayanan.delete');
});
and my controller :
public function edit(GroupService $groupService, $id)
{
$groupService = $groupService->findOrFail($id);
return view('dashboard.gruplayanan.edit', [
'item' => $groupService
]);
}
I am having trouble running the following code to update my database with the form input the user has filled in. The ideal output would be to be redirected to my pageManagement.blade after updating the record in the database. The current output is an error message: Call to a member function update() on string.
The code I have used is shown below.
PageController#update function.
public function update($URI)
{
$data = request()->validate([
'title' => 'required',
'URI' => 'required|min:5|max:10',
'pageContent' => 'required'
]);
$URI->update($data);
return redirect('/p');
}
editPage.blade.php
<h1>Fill in the form to edit a page below.</h1>
<form action="/page/{{ $pageContent->URI }}" method="post">
#csrf
#method('PATCH')
<label for="title">Title:</label><br>
<input type="text" id="title" name="title" autocomplete="off" value="{{ $pageContent -> title
}}"><br>
#error('title') <p style="color: red">{{ $message }}</p> #enderror
<label for="URI">URI:</label><br>
<input type="text" id="URI" name="URI" autocomplete="off" value="{{ $pageContent -> URI }}">
<br>
#error('URI') <p style="color: red">{{ $message }}</p> #enderror
<label for="pageContent">Page Content:</label><br>
<textarea id="pageContent" name="pageContent" value="{{ $pageContent -> pageContent }}">
</textarea>
#error('pageContent') <p style="color: red">{{ $message }}</p> #enderror
<input type="submit" value="Submit">
</form>
THE SCRIPT SRC SHOULD BE HERE BUT HAS NOT BEEN INCLUDED DUE TO THE INDENTATION ISSUE WITH
STACKOVERFLOW.
<script>
tinymce.init({
selector:'#pageContent'
})
</script>
Web.php file where I store my routes.
Route::patch('/page/{URI}','PageController#update');
My GitHub repository link is attached below if you want a better view of the code.
https://github.com/xiaoheixi/wfams
Thanks for the help everyone!
The error is coming from a non-instantiated object URI. You are passing a string from your form - whatever $pageContent->URI is. Without loading the object you wish to update, it is just a string, thus the error message.
$URI->update($data);
at the end of your method is basically saying something like 'call update on the number 32' (32->update($data)).
To fix the error in your question, you can instantiate the object at the beginning of your method:
public function update($URI)
{
$data = request()->validate([
'title' => 'required',
'URI' => 'required|min:5|max:10',
'pageContent' => 'required'
]);
$obj = \App\URIModel::find($URI); // Whatever the model is
$obj->update(request()->all());
return redirect('/p');
}
Alternate Fix: Since you have the named variable inside the route file, you can also take advantage of route-model binding by injecting the model directly in the method() to save a line of code:
public function update(\App\URIModel $URI)
{
$data = request()->validate([
'title' => 'required',
'URI' => 'required|min:5|max:10',
'pageContent' => 'required'
]);
$URI->update(request()->all);
return redirect('/p');
}
I am a newbe of Laravel 6. I made an app to create meetings.
The user creates a meeting by a form with many inputs and in the controller there is a validator that checks if the inputs are filled in.
Every field works well with the validator except for the two inputs type time ("start" and "end").
When I submit the form and I let one of those fields blank the validator doesn't work correctly because it appears an exception message:
InvalidArgumentException Illegal operator and value combination.
Furthermore in the tools for developers appears the following error:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
I tried changing required with required|date_format:H:i for both fields but it didn't work as well.
I tried even removing CheckTime that is a custom rule that checks if the end of the meeting be after the beginning but I failed.
input "start"
<label for="start" class="col-sm-2">Start Hour:</label>
#if (old('start') !== null)
<input type="time" class="form-control col-sm-6" id="start" name="start" value="{{ old('start') }}">
#elseif (isset($id))
<input type="time" class="form-control col-sm-6" id="start" name="start" value="{{ $meeting->start_hour }}">
#else
<input type="time" class="form-control col-sm-6" value = "{{ old('start') }}" id="start" name="start">
#endif
input "end"
<label for="end" class="col-sm-2">End Hour:</label>
#if (old('end') !== null)
<input type="time" class="form-control col-sm-6" id="end" name="end" value="{{ old('end') }}">
#elseif (isset($id))
<input type="time" class="form-control col-sm-6" id="end" name="end" value="{{ $meeting->end_hour }}">
#else
<input type="time" class="form-control col-sm-6" value = "{{ old('end') }}" id="end" name="end">
#endif
validator:
public function insert_meeting(Request $request)
{
$this->validate($request, [
'participants' => [ 'required', new CheckParticipantInsert() ],
'description' => 'required',
'room' => [ 'required', new CheckRoomInsert() ],
'date_meeting' => [ 'required', new CheckDateTime() ],
'start' => [ 'required', new CheckTime() ],
'end' => 'required',
]);
$participants_mail = $this->convertIdToName(request('participants'));
$mail_response = $this->send_mail_create($request, $participants_mail);
$meeting = new Meeting();
$participants = request('participants');
$meeting->id_participants = implode(';', $participants);
$meeting->description = request('description');
$meeting->id_room = request('room');
$meeting->date = request('date_meeting');
$meeting->start_hour = request('start');
$meeting->end_hour = request('end');
$meeting->save();
if($mail_response) {
$message_correct = "The meeting has been correctly inserted. The emails to advise the participants have been sent.";
return redirect()->route('home')->with('success', $message_correct);
} else {
$message_correct = "The meeting has been correctly inserted.";
$mail_not_sent = "The emails to advise the participants could not be sent";
return redirect()->route('home')->with('success', $message_correct)->with('mail_not_sent', $mail_not_sent);
}
}
In theory both fields are required so I don't understand why appears an exception error and by the way why the validator doesn't let simply a similar message in the view: The start field is required or . The end field is required . Is someone able to help me?
I have login form with two text fields email, password.when I tried to login with credentails it's working fine but when clear cache and then tried to login it gives the 'MethodNotAllowedHttp' exception.I am not getting the issue why it's showing this error. My code is as follows:
Route::post('users/login/5', 'administrator\usersController#login')->name('sl');
usersController.php
namespace App\Http\Controllers\administrator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Validator;
use Session;
class FreeusersController extends Controller {
function login(Request $request) {
$type = $request->segment(3);
//print_r($request);
echo "type=".$type;
echo $request->input('email');
echo $request->input('password');
die("sss");
if ($request->isMethod('post') && !empty($type)) {
$this->validate($request, [
'email' => 'required|min:5,max:50',
'password' => 'required|min:5,max:50'
]);
switch ($type) {
case 5:
$condArr = ['email' => $request->input('email'), 'password' => $request->input('password'), 'type' => '5', 'role' => 'father'];
break;
case 4:
$condArr = ['email' => $request->input('email'), 'password' => $request->input('password'), 'type' => '4', 'status' => true];
break;
}
if (Auth::attempt($condArr)) {
return redirect('administrator/dashboard');
} else {
return redirect(url()->previous())->withErrors(['password' => 'Invalid credentials'])->withInput();
}
} else {
return redirect("/");
}
}
}
<form action="/users/login/5" id="login-form" method="post" class="smart-form client-form">
{{ csrf_field() }}
<fieldset>
<section>
<label class="label">Email</label>
<label class="input"> <i class="icon-append fa fa-user"></i>
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}">
</section>
<section>
<label class="label">Password</label>
<label class="input"> <i class="icon-append fa fa-lock"></i>
<input id="password" type="password" class="form-control" name="password">
</section>
</fieldset>
<footer>
<button type="submit" class="btn btn-primary">
LogIn
</button>
</footer>
</form>
You have to clear routes:
php artisan route:cache
Then it will remember that login action is post.
I think you are getting this error because you when you are calling this url there is another url with something like users/{id} which means anything after users/. May be you are using resource route. So, when you call url users/login/5 its taking login/5 as $id of that users/{id} url.
But that url is for GET method. You are calling this with post method, as a result you are getting this error.
Solution
You can try calling your url using route method like:
action="{{route('sl', ['id'=>5])}}"
If it doesn't work then you can change your route to something else. For example:
Route::post('user/login/5', 'administrator\usersController#login')->name('sl');
Here you can use user instead of users because you already have a url with a users. Don't forget to change your action too.
First look at your routes with php artisan route:list.
Then are you sure of your request verb ? Look in your navigator console to look at your requests.
I am creating referral system so I have the following routes
// Registration routes...
Route::get('auth/register/{id}', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
and my RegisterUser.php is changed to
public function getRegister($id)
{
return view('auth.register')->withName($id);
}
and my blade looks like
<div class="form-group">
<label class="col-md-4 control-label">Company</label>
<div class="col-md-6">
<input type="text" class="form-control" name="company" value="{{ old('company') }}" readonly disabled>
</div>
</div>
in AuthController I have:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'company' => $data['company'],
'password' => bcrypt($data['password']),
]);
}
and the value="{{ old('company') }}" is causing the problem. When it is like that it works. But I want the value to be value="{{$name}}" given from return view('auth.register')->withName($id); So when I go to route auth/register/something in the input field I have got the 'something' so it is working but I have the error code "Undefined index: company". When I remove the value at all it is working but I need this value. Any suggestions would be helpful.
The Problem of your code is the disabled attribute in the input "company", why ? well a disabled element isn't editable and isn't sent on submit. so Laravel doesn't receive it so you will be able to access via the helpers old.
Remove the disabled attribute and the magic happens.
<input type="text" class="form-control" name="company" value="{{ old('company') }}" readonly >