Laravel match and any method - php

I´m working with Laravel building a web app. This app will contain a FORM that only asks a name of a requisite of a client that will be added in to a database. I´m trying to obtain the ID of the client by 'GET' method and the title of the requisite by 'POST' method. I´ve tryied to use 'MATCH' metchod but that brings me errors.
These is the code in the view:
<form action="{{ route('folders.store') }}" method="post">
#csrf
#method('match')
...
</form>
These in the web:
Route::match(['get', 'post'], 'folders/store', [PartnerController::class, 'store'])-> name('folders.store');
and these in the controller:
public function store(Request $request)
{
#dump($request);
return $request;
}
In the last I´m just trying to verify want I´m doing. jaja.
Thank you.
I´ve tryied using method 'any', with no results.
I´ve changed the 'post' to 'get' method but it´s not a really secure option.
I think I don´t really understand what I´m doing at all.

you're out of conventions but anyway if you're user logs in you can get the id with Auth::id(), but if not, go with this
Route::post('folders/store/{$id}', [PartnerController::class, 'store'])-> name('folders.store');
then in form
<form action="{{ route('folders.store',$id) }}" method="post">
#csrf
...
</form>
and in the controller
public function store(Request $request,$id)
{
dd($request);
return $request;
}

Related

Laravel Routing does not Controller

I want to access my controller and show and log the test messages.
I have been trying to access my controller via a route but it doesn't seem to work.
It's my first time using Laravel so I hope anyone can help I have been struggling with this problem for a couple of hours.
It hasn't been showing error messages, but I can't find any mistakes in my syntax I tried finding if there might be possible problems in my php.ini files, but I don't know.
the form
<form method="post" action="{{ Route('saveItem') }}" accept-charset="UTF-8">
{{ csrf_field() }}
<label for="listItem">New Todo Item</label><br>
<input type="text" name="listItem">
<button type="submit">Save item</button>
</form>
Route
Route::post('/saveItemRoute', [TodoListController::class, 'saveItem'])->name('saveItem');
Controller
class TodoListController extends Controller
{
public function saveItem(Request $request) {
echo("<h1>victory2</h1>");
\Log::debug("TodoListController");
\Log::info(json_encode($request->all()));
$newListItem = new ListItem;
$newListItem->name = $request->ListItem;
$newListItem->is_complete = 0;
$newListItem->save();
return view('welcome');
}
}
After trying again it seem like the rout doesnt function / do anything and i cant figure out why
I know what whent wrong after changes to routing i needed to do this line in the cmd
php artisan route:clear
the first time i tested the routing code but i did not know after changes you need to clear the route

Laravel 9 html form submit throws 405. Expects PUT, request is GET

I'm trying to create a simple CRUD application with Laravel 9. I've run into a problem with HTML forms. I've created a page where you can edit data on rabbits existing in my database.
My form
<form name="editRabbitForm" action="{{ url('/rabbit/update') }}" method="PUT">
{{ csrf_field() }}
<!-- here be input fields -->
<button type="submit" class="btn btn-success">Save</button>
<a type="button" href="/rabbits" class="btn btn-danger">Cancel</a>
</form>
web.php routes
<?php
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\QuoteController;
use App\Http\Controllers\RabbitController;
use Illuminate\Support\Facades\Route;
Route::get('/rabbits', [RabbitController::class, 'index']);
Route::get('/rabbit/edit/{id}', [RabbitController::class, 'edit']);
Route::put('/rabbit/update', [RabbitController::class, 'update']);
RabbitController.php
<?php
namespace App\Http\Controllers;
use App\Models\Rabbit;
use Illuminate\Http\Request;
class RabbitController extends Controller
{
public function index() {
return view('rabbits.index', ['rabbits' => Rabbit::all()]);
}
public function edit($id) {
return view('rabbits.edit', ['rabbit' => Rabbit::where('breed_id', $id)->first()]);
}
public function update(Request $request) {
echo("SOME SORT OF RESULT!");
var_dump($request);
}
}
Before I even hit the controller I get an exception reading:
The GET method is not supported for this route. Supported methods: PUT.
I really don't get what I'm doing wrong in this scenario
As stated above in my comment:
To send a put request you will need to change the method to POST and add #method('PUT') in your form. This will insert a hidden input for the method. Laravel will then automatically route this request to the method specified by the put route in your routes file.
This is needed because as #ADyson writes, browsers are limited to GET and POST request.
And last but not least, browsers or in this case HTML forms are stupid.
Maybe someday this will be changed, who knows.

How to use <form> action to redirect to method in routing using LUMEN

Lumen does not use Collective/html. When I try to use form action :
I got error ** Call to undefined function action()
How can I send form to specific method in my route ?
I have controller and store method.
I have routing to my controller
I have form and want to point to specific method
My form :
<form action="{{ action('LicensesController#store') }}" method='POST'>
but i got error Call to undefined function action()
My controller's method :
// saving to database
public function store(Request $request){
}
My routing :
on page /licenses/create I have form and want to send it to #store method (where I will save it to database
Route::get('/licenses', 'LicensesController#index');
Route::post('/licenses', 'LicensesController#store');
Route::get('/licenses/create', 'LicensesController#create');
Route::get('/licenses/{licence}', 'LicensesController#show');
How can I send form to specific method in my route using LUMEN (where a lot of Laravel's function does not work)
I can use
<form action="/licenses" method='POST'>
and it works , as routing will fire store method but I think there is more proper way , more eloquent , and you can help me find it

Can not get my data from my form in Laravel?

I am new to Laravel, I am trying to have a simple example, but I am getting a 419 error, I dont know why it shows up but I will expalin what I did,
I created a simple Controller and I called it FormController with the command line :
php artisan make:controller --resource FormController
In my web.php I added this :
Route::resource('form','FormController');
my view has a simple form in it :
<form action="/form" method="POST" >
<input type="text" name="cih">
<input type="submit">
</form>
I open my view with the create method :
public function create()
{
return view('contact');
}
I want that when I submit my form I get my data, so I use my 'store' method :
public function store(Request $request)
{
return $request->all();
}
But instead of getting it, I get 419 message, and my session has expired etc ..
I followed a course and that what the teacher was doing I believe nothing more, so I would appreciate any help, I need it.
Thank you
You need to include the CSRF token while submitting a form since the 'VerifyCsrfToken' middleware is enabled by default for the web routes in App/Http/Kernal.php.
<form action="/form" method="POST" >
#csrf
<input type="text" name="cih">
<input type="submit">
</form>
And, Welcome to Laravel!

laravel 5, form submit redirect to unknown url value

I just wondering in my project. I have a form that can be access at localhost/app/esetting/mymail and this is the code in the view:
....
<form action="{{ url( 'app/esetting/emailautomsave' ) }}" class="form-horizontal" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
...
<input type="submit" value="save">
</form>
but when I try to click for form submission, I expect it to go to app/esetting/emailautomsave and calls it controller which is on my SettingsController.php.
public function postEmailautomsave(Request $request){
...
}
but it redirects to localhost/app/mymail? and give me this error:
NotFoundHttpException in Controller.php line 93:
Controller method not found.
this sounds weird on my end. can anyone have an idea about this? I am sure I have done the right this specially on my routes.php
Route::group([ 'prefix' => 'app', 'middleware' => 'auth' ], function() {
....
Route::controller('esetting', 'SettingController');
Route::get( 'esetting/mymail', 'SettingController#viewEmailAutom' ); // view for the form to display
....
Just simply define:
Route::post('esetting/mymail/emailautomsave', 'SettingController#postEmailautomsave');
I know that You'll say: "I've defined Route::controller, so it will look for it atuomatically."
But for me is best to have routes defined exactly.
also if:
it redirects to localhost/app/mymail? and give me this error:
NotFoundHttpException in Controller.php line 93: Controller method
not found.
maybe it means that some middleware is redirecting You there?
You can check it by simply doing this:
public function postEmailautomsave(Request $request){
die('test');
...
}
if it will redirect so it mean that some function was called before and redirect browser to app/mymail.
If you use Route::controller you need to define you methods according to the route.
E.g.
Route::controller('esetting', 'SettingController');
Will look for SettingController#getIndex when you visit http:://yoursite.com/esetting
To reach SettingController#postEmailAutomsave() you would need to go to the route http:://yoursite.com/esetting/emmail/automsave
I have not used Route::controller myself, I got used too (and now prefer) naming them individually.
But this should do the trick.
Laravel 5.1 docs

Categories