Symfony\Component\Routing\Exception\RouteNotFoundException: Route [Lala.search] not defined - php

I created a model and its migration like this:
php artisan make:model Lala -m
And I made this : php artisan migrate
I was going to call this road, but I have a mistake. Did I write it wrong? How can I call the search method when my form is submitted?
formular:
<?php
use App\Models\Lala;
?>
<form action="{{ route('Lala.search')}}" method="GET" >
<div class="input-group mb-3">
<input type="text"
name="name" class="form-control" placeholder="Geben Sie etwas an"
aria-label="Geben Sie etwas an"
aria-describedby="basic-addon2" autocomplete="off">
<div class="input-group-append">
<span class="input-group-text" id="basic-addon2">🎓</span>
</div>
</div>
<input type="submit" class="btn btn-primary" value="search">
</form>
I defined the route as follows in web.php :
use App\Models\Lala;
Route::get('/search',[
'as' =>'Lala.search',
'uses' =>'\App\Http\Controllers\stipendiensController#search']);
stipendiensController is defined like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Stipendien;
class stipendientsController extends Controller
{
public $name;
public function search()
{
return view('seite.Stipendien');
}
}
how to avoid this error? could I write this code differently? I try indeed to enter the data in my search bar and I compare in my database if the value entered in the search bar is there.
Thank you for helping me . Please

Try defineing your route like so:
use App\Http\Controllers\stipendiensController;
Route::get('/search', [stipendiensController::class, 'search'])
->name('Lala.search');

Related

The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel 9 [duplicate]

This question already has answers here:
The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel
(19 answers)
Closed 8 months ago.
First off, I'm a beginner and I know this question has been asked a few times, and answered. I've attempted the solutions, or I've had the suggestion implemented, so I've come here for help from the experts!
What I've tried:
-Adding #csrf to my blade file inside my form element.
-Checking that my post and get's in my routes are lowercase.
-Changing the gets to post and so fourth.
The above has not helped in solving the problem, and alas I'm still scratching my head.
Effectively, I'm trying to upload an image, give the image a caption and submit. It should go to another page displaying the data in a drop down in the top left hand corner, but I'm greeted with the image in the title.
EDIT: Solution: I ran the following and it removed the error:
php artisan route:clear
I've included images below of the error and my folder structure:
Image of error appearing.
Project directory structure
Here's the code:
Web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/p/create', 'App\Http\Controllers\PostsController#create');
Route::post('/p/', 'App\Http\Controllers\PostsController#store');
Route::get('profile/{user}', 'App\Http\Controllers\ProfilesController#index')-
>name('profile.show');
PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class PostsController extends Controller
{
public function constructor()
{
$this->middleware['auth'];
}
public function create()
{
return view('posts.create');
}
public function store() {
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
]);
dd(request('image')->store('uploads','public'));
auth()->user()->posts()->create($data);
dd(request()->all());
}
}
Model/Posts.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
views/posts/create.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<form action="/p" enctype="multipart/form-data" method="post">
#csrf
<div class="row">
<div class="col-8 offset-2">
<div class="row mb-3">
<div class="row pt-5">
<h1>Add New Post</h1>
</div>
<label for="caption" class="col-md-4 col-form-label">Post Caption</label>
<input id="caption"
type="text"
class="form-control #error('caption') is-invalid #enderror"
name="caption"
value="{{ old('caption') }}"
autocomplete="caption" autofocus>
#error('caption')
<strong>{{ $message }}</strong>
#enderror
</div>
<div class="row">
<label for="image" class="col-md-4 col-form-label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
#error('image')
<strong>{{ $message }}</strong>
#enderror
<div class="pt-3">
<button class="btn btn-primary btn-sm">Add New Post</button>
</div>
</div>
</div>
</div>
</form>
#endsection
Thank you in advance for your help!
Could this be caused by your route being defined as "/p/", while your form action is just "/p" (without the trailing slash)? I'd try ensuring those are an exact match first. Change your route to say:
Route::post('/p', 'App\Http\Controllers\PostsController#store');
You could also try broadening that route definition.
Route::post('/p/', 'App\Http\Controllers\PostsController#store');
would become
Route::any('/p/', 'App\Http\Controllers\PostsController#store');
EDIT: Solution: I ran the following and it removed the error:
php artisan route:clear
you need to use and name for your router, and now route will look like
Route::post('p',[PostsController::class, 'store'])->name('p.name');
And action form:
<form action="{{route('p.name')}}" enctype="multipart/form-data" method="post">
And clear cache route then change:
php artisan route:cache

Can't get input data from form LARAVEL

I'm learning Laravel and I got stuck trying to get data from a form.
I already am able to get data back with GET, but with POST I've been having a ton of trouble. Here's what I'm working with:
Form:
<form id="forms" method="POST" action="sugestoes" novalidate>
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-12">
<label for="obs">Observações:</label>
<textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
</div>
</div>
<hr>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
#php
if (isset($_POST["obs"])) {
echo "IN";
}
#endphp
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$name = $request->input('obs');
return redirect('sugestoes');
//
}
}
Route:
Route::post('sugestoes', 'PostController#store');
The intended behaviour that I'm trying to reach is for the post to be submitted, and then returning to the same page with an empty form. Later on I'll be sending the input data into a database, but for now I just want to get the post to work.
I guess I'm missing something really basic, but I've been following guides and looking online, I've done some progress but I'm really stuck here.
(some more info, this is Laravel 5.4, and I'm using XAMPP)
First, you need to call the model, use App/Your_model_name; then you have to save the data.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Suggest; //Suggest model, let's hope you have suggest table
class PostController extends Controller
{
public function store(Request $request)
{
$suggest = new Suggest; //model
$suggest->name = $request->obs; //name is DB name, obs is request name
$suggest->save(); //save the post to DB
return redirect()->back()->with('success', 'Saved successfully'); //return back with message
}
}
Then if you want to flash the message on the HTML page
#if(session('success'))
<div class="alert alert-warning alert-dismissible" id="error-alert">
<strong style="color: white;">{{session('success')}}</strong>
</div>
#endif
<form id="forms" method="POST" action="{{ route('sugestoes') }}" novalidate>
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-12">
<label for="obs">Observações:</label>
<textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
Remove the #php tag below the form, then in router.php
Route::post('/sugestoes', 'PostController#store')->name('sugestoes');
Then in Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$name = $request->input('obs');
return redirect('/sugestoes'); // you should have GET in Route.php
//
}
}
Add the following code in your action attribute on the form. It will capture the post URL. When you submit the form it will send the form data to the URL end-point.
action="{{ url('sugestoes')}}"
Then die and dump in your controller store function
public function store(Request $request)
{
dd($request->all());
}

Laravel form include input in action attribute

LARAVEL 5.0
PHP 5.4.45
I have a route that is shaped like this :
/app/Http/routes.php
Route::get('/clients/search/{id}', 'ClientController#searchById')->where('id', '[0-9]+');
Route::get('/clients/search/{text}', 'ClientController#searchByText')->where('text', '[a-zA-Z]');
I will not print my view here but it simply search for the exact client (case id) or the first 10 clients (case text).
Then I want to create a search form. I created the route :
/app/Http/routes.php
// Route::get('/clients/search/{id}', 'ClientController#searchById')->where('id', '[0-9]+');
// Route::get('/clients/search/{text}', 'ClientController#searchByText')->where('text', '[a-zA-Z]');
Route::get( '/clients/search', 'ClientController#search');
The controller for this route :
/app/Http/Controllers/ClientController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ClientController extends Controller {
// Controllers for the 'searchById' and 'searchByText'
public function search() {
return view('client.search.search', [
'title' => 'Client search form',
'title_sub' => ''
]);
}
}
And the view for this search form :
/ressources/view/client/search/search.blade.php
<form action="/clients/search/INPUT HERE ?" method="get">
<div class="input-group">
<input id="newpassword" class="form-control" type="text" name="password" placeholder="Id de client, nom, prénom, ...">
<span class="input-group-btn">
<button id="button_search" class="btn green-haze" type="submit"><i class="fa fa-search fa-fw"></i></button>
</span>
</div>
</form>
QUESTION
How can I, before submit, pass an input as a part of my action attribute for my form ? The point is to be able to launch those kind of requests :
/clients/search/26
/clients/search/Mike%20%Folley
/clients/search/Paris
So my controllers handling this route could do the job. Is there any way to do that ? Or should I go for JavaScript solution (which make me sad a bit) ?
Yes, you need javascript to modify request URL before it is even sent :). No form tag needed for that, vanilla js approach like this might be sufficient:
<input type="text" id="search">
<button onclick="search()">
Submit
</button>
<script>
function search() {
window.location='/search/' +
encodeURIComponent(document.getElementById('search').value);
}
</script>
You can and should just use one route and then in controller's method decide on what to search and what view to return. Routes should contain the least amount of logic possible.

Laravel 5.1 Trying to post data to controller but geting MethodNotAllowedHttpException error

Im trying to POST data to my controller but I'm getting a
MethodNotAllowedHttpException in RouteCollection.php line 219:
error message, here are my files.
my route file
<?php
Route::get('/', function () {
return view('welcome');
});
// Authentication routes
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes
Route::get('register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::controllers(['password' => 'Auth\PasswordController',]);
Route::get('/home', 'HomeController#index');
// Using A Route Closure
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
Route::auth();
}]);
// practicing using forms for sending data to the DB & populating form fields with DB data
Route::get('profile', 'ProfileController#index');
Route::post('profile/update', 'ProfileController#updateProfile');
profile.blade.php
<form method="POST" action="/profile/update/">
<div class="form-group hidden">
<input type="hidden" name="id" value="<?php echo $users[0]->id;?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
</div>
<div class="form-group">
<label for="email"><b>Name:</b></label>
<input type="text" name="name" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<label for="email"><b>Email:</b></label>
<input type="text" name="email" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Submit </button>
</div>
</form>
& my ProfileController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
/**
* Update user profile & make backend push to DB
**/
public function index()
{
if(Auth::check()) {
// connecting to the DB and accessing
$users = User::all();
//var_dump($users);
return view('profile', compact('users'));
}
return view('auth/login');
}
public function updateProfile(Requests $request) {
return $request->all();
}
}
not sure what the issue is. Thanks for all the help everyone
A couple of issues that we managed to address here:
Over-use of HTTP Verbs
At your view, you have: <form method="POST" but also <input name="_method" type="hidden" value="PATCH"> which may conflict between a POST and a PATCH. Since your routes.php only declares POST, let's remove the patch definition.
Routing Mistake
Still at your view, your action points to action="/profile/update/" while your route is defined as Route::post('profile/update'), notice the extra / at the end in your form. That slash should not be there.
Controllers Request
You have a here: use App\Http\Requests; is probably incorrect because that's a folder within Laravel, not a class. Let's remove that and keep use Illuminate\Http\Request; for now. In the near future, you'll be learning how to create your own Form Requests and you'll probably want a UpdateProfileRequest.php.

Laravel- Form post going to wrong URL

I am learning Laravel from scratch.
I am doing simple form "POST" but something is missing and I am not able to find out what is missing.
So here is my "routes.php":
Route::get('cards','CardsController#all');
Route::get('cards/{card}','CardsController#show');
Route::post('cards/{card}/notes','NotesController#new');
and view:
<div>
<form method="POST" action="cards/{{ $card->id }}/notes">
<div class="form-group">
<label>Note body:</label>
<textarea name="body" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
and here is controller:
`
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function new(Request $request){
return $request->all();
}
}
`
Now when I submit the form it gives me following error:
NotFoundHttpException in RouteCollection.php line 161
and the URL in browser becomes:
http://localhost:88/learning/cards/cards/1/notes
which is definitely wrong.
I think I am missing something very basic.
Please guide me.
Thank you.
Change the form action to,
<form method="POST" action="{{ url('cards/'.$card->id.'/notes') }}" >
Your action starts with "cards/". Change the action to start with '/cards/' so that it's path is not calculated relative to the current route.
<form method="POST" action="/cards/{{ $card->id }}/notes">

Categories