Laravel redirecting to the wrong route - php

web.php
Route::get('/gigs/{id}', [GigsController::class, 'info'])->name('clientside.gigs_info');
Route::get('/gigs/create', [GigsController::class, 'create'])->name('clientside.gigs.create');
Controller
public function create()
{
$categories = Category::select('id', 'name')->get();
return view('clientview.gigs.create', compact('categories'));
}
public function info($id)
{
$gig = Gigs::join('users', 'users.id', '=', 'gigs.created_by')
->join('categories', 'categories.id', '=', 'gigs.category_id')
->select('gigs.*', 'categories.name as category_name', 'users.name as user_name', 'users.surname')
->where('gigs.id', '=', $id)
->orderBy('name', 'ASC')
->firstOrFail();
return view('clientview.gigs.info', compact('gig'));
}
When I try to click this:
<a class="dropdown-item" href="{{ route('clientside.gigs.create') }}">Create Gigs</a>
When I click this I can observe from DebugBar that it directs to route ('clientside.gigs_info')
I think "/create" thinks it is an /{ID} but however, I direct to a different route

Answer by Michael Mano,
Make sure you write on web.php static routes before dynamic.

Just write create route before info route because it is dynamic route (That accept parameter) so always write dynamic route after the static route.

You actually created a dynamic route gigs/{id} so anything that comes after gigs will be called as a parameter of gigs. So to fix this change the order in your web.php like below. So it will search for static route first and then go for dynamic route.
Route::get('/gigs/create', [GigsController::class, 'create'])->name('clientside.gigs.create');
Route::get('/gigs/{id}', [GigsController::class, 'info'])->name('clientside.gigs_info');

Related

Laravel 7, Missing required parameters for [Route: /xxxxx] error

I am trying to pass a value from a view to a controller and use that value in a query. I am getting Missing parameter error. This is my code.
View:
A
Route:
Route::get('/first_letter/{$f_letter}','PtoductController#first_letter')
->name('first_letter');
Controller:
public function index()
{
$products = DB::table('products')
->join('families', 'products.id_family', '=', 'families.id')
->select('products.name')
->where('products.name', 'like', $f_letter.'%')
->get();
return view('product.index', compact('products'));
}
You have two problems
first, fix your route to be like this
Route::get('/first_letter/{f_letter}','PtoductController#first_letter')
->name('first_letter');
when you pass a wildcard in route it is written like this {f_letter} not {$f_letter}
Second, in your controller you need to pass the wildcard in your method as parameter like this
public function index($f_letter)
{
$products=DB::table('products')
->join('families', 'products.id_family', '=', 'families.id')
->select('products.name')
->where('products.name', 'like', $f_letter.'%')
->get();
return view('product.index', compact('products'));
}
you can read more about routing parameters here
At last you can make your a tag more clear like this
A
First, are you sure the controller name is "PtoductController"? Shouldn't it be called ProductController?
Then, if the controller name is correct, remove "$" from the link you call from the controller.
Route::get('/first_letter/{f_letter}', ['uses' => 'PtoductController#first_letter'])
->name('first_letter');
Last, Put $f_letter as a parameter in the "first_letter" function.
public function index($f_letter)
{
//your code
}

Subpage In Laravel?

I tried applying some solutions I've found online but I can't seem to have any of them work for me. (I'm fairly new to Laravel) I'm trying to get Laravel to direct to blade views I set up. However, I want to be able to access them via sub-page urls. Could I get some help? The URLs I'm using to at the moment are
/course/{course_slug}
/lesson/{lesson_slug}
/module/{module_slug}
I have course.blade.php , lesson.blade.php, and module.blade.php set up. (not sure why it doesn't let me post my code)
But I want to be able to route to these views using this syntax:
/course/{course-slug}
/course/{course-slug}/{lesson-slug}
/course/{course-slug}{lesson-slug}/{module-slug}
Any suggestions would be great. Thank you so much.
Erwin
EDIT: Code used for blade files (changing out the module variable to lesson, course, etc)
#foreach($modules as $modules)
<li>
<a href="/modules/{{ $lesson->lesson_slug}}/{{ $modules->module_slug }}">
<span>{{ $modules->module_title }}</span>
</a>
</li>
Here's my routes file code:
Route::get('course/{slug}', function($slug){
$course= App\Course::where('slug', '=', $slug)->firstOrFail();
$lessons = App\Lesson::where("course_id", "=", $course->id)->get();
return view('course', compact('course'))->with('lessons', $lessons);
});
Route::get('lesson/{slug}', function($slug){
$lesson = App\Lesson::where('lesson_slug', '=', $slug)->firstOrFail();
$modules = App\Module::where("lesson_id", "=", $lesson->id)->get();
return view('lesson', compact('lesson'))->with('modules', $modules);
});
Route::get('module/{slug}', function($slug){
$module = App\Module::where('module_slug', '=', $slug)->firstOrFail();
return view('module', compact('module'));
});

Route precedence order

I have 2 routes with their methods written in same controller[LinkController]:
Route::get('/{country}/{category}', ['as' => 'tour.list', 'uses' => 'LinkController#tourlist']);
Route::get('/{category}/{slug}',['as' => 'single.tour', 'uses' => 'LinkController#singleTour']);
And my methods are:
public function tourlist($country, $category)
{
$tour = Tour::whereHas('category', function($q) use($category) {
$q->where('name','=', $category);
})
->whereHas('country', function($r) use($country) {
$r->where('name','=', $country);
})
->get();
return view('public.tours.list')->withTours($tour);
}
public function singleTour($slug,$category)
{
$tour = Tour::where('slug','=', $slug)
->whereHas('category', function($r) use($category) {
$r->where('name','=', $category);
})
->first();
return view('public.tours.show')->withTour($tour);
}
My code in view is:
{{$tour->title}}
The trouble i am having is the second route [single.tour] returns the view of the first route [tour.list]. I tried to return other view also in 2nd method but still it returns the view of first method. Does laravel have routing precedence ?
This is happening because, Laravel matches routes from the file, and the route which comes earlier and matches the pattern will execute first, you can use regex pattern technique to avoid this like:
Route::get('user/{name}', function ($name) {
//
})->where('name', '[A-Za-z]+'); // <------ define your regex here to differ the routes
See Laravel Routing Docs
Hope this helps!
Both your routes consist of two parameters in the same place. That means any url that matches route 1 will also match route 2. No matter in what order you put them in your routes definition, all requests will always go to the same route.
To avoid that you can specify restrictions on the parameters using regular expressions. For example, the country parameter may only accept two letter country codes, or the category parameter may have to be a numeric id.
Route::get('/{country}/{category}')
->where('country', '[A-Z]{2}')
->where('category', '[0-9]+');
https://laravel.com/docs/5.3/routing#parameters-regular-expression-constraints

Routing with parameters in laravel 5

I've got a results page for my website that outputs a list of users.
#extends('templates.default')
#section('content')
<h3>Results for "{{ Request::input('query') }}"</h3>
#if (!$users->count())
<p>No results found, sorry.</p>
#else
<div class="resultRow">
<div class="">
#foreach ($users as $user)
#include('user/partials/userblock')
#endforeach
{!! $users->appends(Request::except('page'))->render() !!}
</div>
</div>
#endif
#stop
with a fairly standard search controller:
class SearchController extends Controller {
public function getResults(Request $request) {
$query = $request->input('query');
$users = User::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%{$query}%")->where('role', '=', 2)
->orWhere('username', 'LIKE', "%{$query}%")->where('role', '=', 2)
->orWhere('profile_text', 'LIKE', "%{$query}%")->where('role', '=', 2)
->orWhere('keywords', 'LIKE', "%{$query}%")->where('role', '=', 2)
->orWhere('tagline', 'LIKE', "%{$query}%")->where('role', '=', 2)
->simplePaginate(1);
return view('search.results')->with('users', $users);
}
}
Now, this works fine and well. If I search for "Jack", I get all the Jacks.
What I want to know now is, would it be possible to have a route with a predefined parameter or query string?
For example, say, on my front page I had a link to all the plumbers in my users.
<a id="plumbers" href="{{ route('search.results')->withQueryOfPlumbers }}">Plumbers</a></li>
Would this be possible? Or should I be outputting my data another way?
If you are just using GET parameters, the route() helper allows you to pass parameters as a second parameter such as: route('search.results', ['user-type, => 'plumbers'])
This will output: http://www.example.com/search/results?user-type=plumbers
You can add a column to your User called profession, then you'd do something like this:
$plumbers = User::where(['profession'=>'plumber', /*other WHERE options*/])->get();
This will return all of the users that are plumbers.
If you're hard coding the id and text of the a link, then you could just do
<a id="plumbers" href="{{ route('search.results') }}/plumbers">Plumbers</a>
And then match the keyword of plumbers in your routing table, see Laravel docs on routing parameters for more info.
Your route would look something like this:
Route::get('/search/{trade?}', ['uses' =>'SearchController#getResults', 'as' => 'search.results']);
You should be able to then inject the $trade variable into your controller. A small aside, I would avoid using raw queries in controllers as much as possible from a design and maintenance perspective and make a "search" helper function in your Eloquent model for users (See Eloquent query scopes).

how we can pass multiply id in laravel

My view.blade.php code here
<a href="{{ url('p_grid') }}/{{($cat_id)}}/{{$row->sub_id}}">
My route code here
Route::resource('p_grid', 'BasicController#p_grid');
And my BasicController code here
public function p_grid(Request $request, $id)
{
echo "success";
if ($id == 1) {
$r = DB::table('sub_category')
->select('*')
->where('cat_id', $id)
->where('sub_status', '1')
->orderBy('sub_id', 'asc')
->get();
$cat_name = DB::table('category')
->where('cat_id', $id)
->get();
$count = DB::table('products')
->where('sub_id', $id)
->count();
return view('buy-and-sell/p_grid', compact('r','cat_name','count','id'));
}
click on anchor tag to show this error
The extra parameter in your URL is causing the 404. Laravel doesn't expect the URL to have multiple id's, because you are using resourceful routing. You will need to append your routes.php file to account for this:
Route::resource('p_grid', 'BasicController#p_grid');
Route::get('p_grid/{category}/{subcategory}', [
'as' => 'category-with-subcategory',
'uses' => 'BasicController#gridWithSubcategory',
]);
And make sure you have a gridWithSubcategory method in your BasicController.php file.
That said, I'd advise you to understand better what Laravel is doing when you declare Route::resource(), because I would question how much of it you really need. It's really just a shorthand for registering ordinary routes (see here for the full list and specifications) like index, create, show, destroy, etc. If you want to see the routes your app actually has, after being parsed by Laravel (including routes not in your routes.php, which could have been registered by third-party packages) type php artisan route:list on a CLI.
Finally, I'd strongly suggest using route names throughout your app for better clarity and portability. The artisan command above will give you route names, so you can use something like this in your views:
<a href="{{ route('category-with-subcategory', ['category' => $cat_id, 'subcategory' => $row->sub_id]) }}">
That way, if you ever want to change your route's footprint, the view won't break (as long as you maintain the parameter requirements).
My view.blade.php code here
<a href="{{ url('p_grid', ['category' => $cat_id, 'subcategory' => $row->sub_id]) }}">
My route code here
Route::get('p_grid/{category}/{subcategory}', [
'as' => 'category-with-subcategory',
'uses' => 'BasicController#p_grid'
]);
And my BasicController code here
public function p_grid(Request $request, $id)
{
echo "success";
}
success image upload and work done
enter image description here

Categories