I am using a form with PATCH method and I have a button link(since i already have a submit button and using same form for both store and update) as
<a class="btn btn-default" href="{{ URL::to( 'pages/edit/' . $vehicle -> id) }}">EDIT</a>
And my route is
Route::patch('/pages/edit/{id}', ['uses' => 'VehicleProcessController#update']);
Controller
public function update($id)
{
$vehicle = Vehicle::find($id);
$input = Input::all();
$vehicle->update($input);
return $input;
}
When i click to link $input returns null and i am getting
MethodNotAllowedHttpException
I am trying to get familiar with L5, how can i fix this ? Any help would be appreciated.
Your <a> link will trigger a GET request, not a PATCH request. You can use JS to have it trigger a PATCH request, or use a <button> or <input type="submit"> to issue one.
Related
I am working on a E-Prescription Application using Laravel 8. I have built a checkout page which will submit a form containing only 1 value "appointment_id" so that after submitting the form by clicking finish the corresponding appointment status will be changed to "Completed" by the controller using the appointment_id. But when Im clicking on the button to trigger the methods it giving me 404 error. I have used POST method. CSRF is also used. Here is my codes ,
checkout.blade.php
<form action="/doctor/appointments/checkout" method="POST">
#csrf
<div class="form-group row">
<div class="col-md-4">
<input type="hidden" name="appointment_id" value="{{$appointment->id}}">
<input type="submit" class="btn btn-primary btn-block" value="SAVE">
</div>
</div>
</form>
some of my routes:
web.php
Route::prefix('/doctor')->name('doctor.')->namespace('Doctor')->group(function(){
//Appointment Routes
Route::get('/appointments/all',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'AllAppointments'])->name('Appointments')->middleware('doctor');
Route::get('/appointments/view',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'ViewAppointment'])->name('Appointment')->middleware('doctor');
Route::post('/appointments/view',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'DeleteAppointment'])->name('DeleteAppointment')->middleware('doctor');
Route::get('/appointments/conversation',[App\Http\Controllers\Doctor\Appointment\ConversationController::class,'ViewConversation'])->name('ViewConversation')->middleware('doctor');
Route::post('/appointments/conversation',[App\Http\Controllers\Doctor\Appointment\ConversationController::class,'SendMessage'])->name('SendMessage')->middleware('doctor');
Route::get('/appointments/requests',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'ShowRequest'])->name('Requests')->middleware('doctor');
Route::post('/appointments/requests',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'RequestHandel'])->name('Handel')->middleware('doctor');
Route::get('/appointments/prescription',[App\Http\Controllers\Doctor\Appointment\PrescriptionController::class,'CreatePrescription'])->middleware('doctor')->name('CreatePrescription');
Route::post('/appointments/prescription',[App\Http\Controllers\Doctor\Appointment\PrescriptionController::class,'AddMedicine'])->name('AddMedicine');
Route::get('/appointments/checkout',[App\Http\Controllers\Doctor\Appointment\CheckoutController::class,'ViewCheckout'])->middleware('doctor')->name('ViewCheckout');
Route::post('/appointments/checkout',[App\Http\Controllers\Doctor\Appointment\CheckoutController::class,'EndAppointment'])->name('EndAppointment')->middleware('doctor');
}
CheckoutController.php
<?php
namespace App\Http\Controllers\Doctor\Appointment;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Appointment;
class CheckoutController extends Controller
{
public function ViewCheckout(Request $request){
$id = $request->input('id');
$medicines = DB::table('medicines')->where('appointment_id', '=',$id)->get();
$appointments = DB::table('appointments')->where('id', '=',$id)->get();
return view('doctor.appointments.checkout',['medicines'=>$medicines,'appointments'=>$appointments]);
}
public function EndAppointment(Request $request){
$id = $request->input('id');
$appointment = Appointment::findOrFail($id);
$appointment->status = 'Completed';
$appointment->save();
return redirect()->to('/doctor/appointments/all')->with('status','Appointment has been completed');
}
}
I have checked my routes by
php artisan route:list
the route is existing there.
I have also cleared routes chaches by ,
php artisan route:clear
still facing the issue.
I have also updated my composer. But thats not solved my problem. All other routes are working fine. New routes are also working except the only one :
Route::post('/appointments/checkout',[App\Http\Controllers\Doctor\Appointment\CheckoutController::class,'EndAppointment'])->name('EndAppointment')->middleware('doctor');
**
Can anybody help me fixing this ?
**
The "id" field is not id it is appointment_id.
Model::findOrFail() will throw an exception if it can't find a record which will get converted to a 404 response.
$id = $request->input('appointment_id');
$appointment = Appointment::findOrFail($id);
The error occurs because of the findOrFail: you are giving it an incorrect id, since in the form you sent the appointment_id but you only try to retrieve id from the request. Change it to:
$id = $request->input('appointment_id');
$appointment = Appointment::findOrFail($id);
you can change the code
<input type="hidden" name="appointment_id" value="{{$appointment->id}}">
to
<input type="hidden" name="id" value="{{$appointment->id}}">
Because id not found in CheckoutController
$id = $request->input('id');
Model::findOrFail If not found id it will throw 404 response
I hope it will help you
I have been fixing this trivial problem a few days, but it didn't solve either.
When I click the detail button on the table, it will display a 404 or not found message.
Controller method
public function detail($id)
{
$data = DB::table('lirik_lagu')->where('id', $id)->first();
return view ('admin.detail-lirik');
}
Route
Route::get('lirik-lagu/detail/{$id}', [LirikLaguController::class, 'detail']);
Blade
<a class="btn btn-success btn-sm" href="{{ url('admin/lirik-lagu/detail', $data->id) }}">Detail</a>
I will try to fix what i think it's wrong in your code.
First you should give the $data to your blade file
Controller method
public function detail($id)
{
$data = DB::table('lirik_lagu')->where('id', $id)->first();
return view ('admin.detail-lirik',['data'=>$data]);
}
Second, your should omit the $ in your Route id parameters
Route
Route::get('lirik-lagu/detail/{id}', [LirikLaguController::class, 'detail']);
Third, when you generate the url url(...) it seems to have an admin prefix but not your route declaration.
Blade
<a class="btn btn-success btn-sm" href="{{ url('lirik-lagu/detail', $data->id) }}">Detail</a>
You are not passing the variable to the view. This is how you do it:
return view('admin.detail-lirik', $data);
If you need to pass multiple variables you can pass an array in the second paramter like this:
return view('admin.detail-lirik, ["varname" => $var]);
I see several problems here, so I'm going to point them out. First one, you never returned the data to your view:
return view ('admin.detail-lirik', $data);
The other is the URL in your blade file. You called admin/lirik-lagu/detail but you defined route without admin in your web.php file. You can remove the admin from your url, or create a name for your route and call it that way:
Route::get('lirik-lagu/detail/{$id}', [LirikLaguController::class, 'detail'])->name('lirik-langu');
And then use it like this
<a class="btn btn-success btn-sm" href="{{ route('lirik-langu', $data->id) }}">Detail</a>
This is error says. Im inserting an static value for now in controller to check if the controller is okay. the code is below
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
//IM JUST inserting static value for now to be able to check if inserting
DataController
public function update(Request $request, $name=null)
{
$insert = new leave([
'bio_id' => '10258',
'vacation_balance' => '25',
'sick_balance' => '25'
]);
$insert->save();
return view('pages/admin/data');
}
Route web.php
Route::post('admin/pages/admin/data', 'Admin\DTRDataController#update');
data.blade.php
<form action="{{url('admin/pages/admin/dtrdata')}}" method="post">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
<input type='text' class='total_undertimes' name='total_undertimes' id='total_undertimes' style='width:70px' />
<input type="submit" class="btn btn-primary" value="Edit" />
</form
What is ther error in my code
Remove
<input type="hidden" name="_method" value="PATCH">
line from your view. This makes your FORM submitted as PATCH method.
FYI:
methodNotAllowed exception throws when request using wrong method.
Ex:) When u POST to URL that is configured as GET in your Route file. In your situation you are requesting POST url using PATCH method.
You are showing the route for the URI admin/pages/admin/data but your form is going to admin/pages/admin/dtrdata. I am not sure what that 'dtrdata' URI is but it doesn't accept the PATCH method.
admin/pages/admin/data != admin/pages/admin/dtrdata
Your Form Url and route are different :
route : admin/pages/admin/data
form : admin/pages/admin/dtrdata
you need to do two correction in your code.
your Form Url and route are different :
route url : admin/pages/admin/data
form_url : admin/pages/admin/dtrdata
methodNotAllowed this error you got because you have submit form using patch method and you define post method in route. so should be used
Route::patch('admin/pages/admin/data', 'Admin\DTRDataController#update');
instead of
Route::post('admin/pages/admin/data', 'Admin\DTRDataController#update');
Route::patch('admin/pages/admin/data', 'Admin\DTRDataController#update');
Hi there once again SO community. I've been developing a site and so far it's going pretty well. But today after a long day searching for a solution I can't understand nor find what the right path is...
I want to click on a button and a profile page where you can edit the fields appear. I can redirect to the page I want but I don't know how to send the user data so I can populate the fields.
Here is my button code on my view
<button class="btn btn-xs btn-warning dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false" style="border-color: black" id="dados_{{ $user->username }}"> Alterar Dados Pessoais
<i class="glyphicon glyphicon-cog"></i>
</button>
Here is the button AJAX request handler
if((this.id).indexOf("dados") != -1){
var content = this.id.replace("dados_", "");
$.get('callPermissions', {usernameSend:content, '_token': $('meta[name=csrf-token]').attr('content'),}, function(data){
window.location.replace('settings');
});
And here is my callPermission Controller
public function callPermissions(Request $request)
{
if($request->ajax()){
$usernames = Input::get('usernameSend');
if(isset($usernames)){
$user = User::Where('username', '=', $usernames)->first();
$returnHTML = view('userOptions.settings')->render();
return view('userOptions.settings');
}else{
Log::warning("Username não existe na base de dados.");
}
}
}
and here my Settings Controller
public function settings(Request $request)
{
return view('userOptions.settings');
}
And here is the route
Route::get('/callPermissions', 'SidebarController#callPermissions');
I know the controller is wrong and from what I've read I should verify if the AJAX is successful and if it is handle i on the AJAX request. But from what I've understand I'm not using the Controller at all (even though it goes there). How should I send the user information (in this case the username, then I can get everything from the database) and then send it to the view? I've been searching and trying out stuff that doesn't work...since the return view("your_view") on the Controller doesn't work.
Sorry if I've been confusing and if you need additional information feel free to ask!
Thanks for your help!!
Edit: If I return this on the controller
return view('userOptions.settings', compact('user'));
and do a replace with the Ajax request as show above and add this to the settings view
<p> {{ $user->name }} </p>
I get the following error Undefined variable: user (View: C:\wamp64\www\siteXL\ideiasxl\resources\views\userOptions\settings.blade.php)
Is there anyway to send the parameters with a compact alike or I need to send it through the link? Was avoiding to show the username on the url.
Edit2: For further clarification, this works as intended
<button onclick="window.location='{{url('/settings/' . $user->username)}}'" type="button" id="dadosPessoais" class="btn btn-default">Alterar Dados Pessoais
<i class="glyphicon glyphicon-wrench"></i>
</button>
but I was trying not to send id's and usernames through the URL.
If this is not achievable it's ok, but if there's a way I can't find it, that's why I'm asking
I think you have to add a parameter in the Route and receive the data in the controller function. I'd do something like this:
Route:
Route::get('/callPermissions/{user}', 'SidebarController#callPermissions');
Controller:
public function callPermissions(Request $request, $user)
{
//get data related to $user
}
Ajax call:
$.get('callPermissions/'+userIdVariable, {usernameSend:content, '_token': $('meta[name=csrf-token]').attr('content'),}, function(data){
window.location.replace('settings');
});
This would send the user id through the route.
To get the user id with JavaScript, you can make a hidden field in the Blade file and set the user id as the value. For example, if you using Form helper:
{{ Form::hidden('user_id', $user->id, array('id' => 'js-user-id')) }}
And then, in the JavaScript, you can get the value using something like this:
var userIdVariable = $('#js-user-id')->val();
I have a form on my page
<form method="post" action="{{url('/vpage')}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="w100">
<button name="hostel1" class="submitBTN addnowBtn" type="submit" value="The Venetian"> Add Now</button>
</div><!--w100-->
</form>
I getting the request printed in my controller like
public function vegaspage(Request $request){
dd($request);
die;
}
I have also have many fields on my page , when the request params comes to browser the submit button value is not coming in request
Any ideas ?
Inside your controller function try this:
Input::get('hostel1', 'NA');
// It will return its value ie `The Venetian` otherwise `NA`
Note: The second parameter of Input::get() is the default value.
This Follow link
only one input value get following
$name = $request->input('name');
Retrieving All Input Data
$input = $request->all();
Note: The easiest way to debug this, is via the Network tab in google chrome. You can see the header response data.
But the reason this is not working is probably because you are doing a POST Request . If you do a GET request you will get the value of the button.
An other reason could be that you are doing the submit trough javascript and doing an e.preventDefault() in that case you are not really sending the request. so PHP doesn't get the value.
Do
$request->hostel1
When you want to dd() your input params, do
dd($request->all());
I realized that anytime I try to set a name and value to a "submit" button, laravel doesn't retrieve the values in the request. So you might want to use a hidden field like this:
<input type="hidden" name="hostel1" value="hostel1">
and retrieve it on the server side like this:
$request->hostel1;
Change button code to:
<input name="hostel1" value="The Venetian" type="hidden">
<button class="submitBTN addnowBtn" type="submit"> Add Now</button>
In the controller, use this to get the value:
$request->hostel1
By using request namespace you can get value of any input parameters and also same for the submit button.
For this you have to include Request in controller like:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//IN your problem you can submit button value as
$submit_button = $request->input('hostel1');
}
}
For more details about the request you can follow:
https://laravel.com/docs/5.3/requests
Thanks