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
Related
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');
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'));
});
I am having an issue by modifying the route for a view. I want instead of /company/id to show /company/id/name
the route:
Route::get('/company/{id}/{name}', 'PagesController#showCompany')->name('company.detail');
show method in controller:
public function showCompany($id){
$company = Company::find($id);
return view('company.show')->with('company', $company);
}
and in the view $companies is from a search controller - and it should get the results with a link to open the view
#foreach($companies as $company)
Show detail
#endforeach
if using only with id like /company/id works. What i am wrong?
A simple an elegant way (i think) is:
{{route('company.detail', ['id' => $company->id, 'name' => strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $company->company_name))}}
You can have a friendly url name. I am sure that there are better solutions out there.
If you have more params in the route you can use an associative array and initialize each param name with a value.
the controller now is the same with the id.
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
Using Laravel 5 I m trying to delete a single record within a controller, here is my code:
public function destroy($id)
{
$employee = Employee::find($id);
$employee->delete();
return Redirect::route('noorsi.employee.index');
}
My view page code is:
<td>Delete</td>
My route is:
Route::delete(employee.'/{id}', array('as' => 'noorsi.employee.destroy','uses' => Employeecontroller.'#destroy'));
That did not work.
How do I fix the implementation ?
From the official Laravel 5 documentation:
Delete an existing Model
$user = User::find(1);
$user->delete();
Deleting An Existing Model By Key
User::destroy(1);
User::destroy([1, 2, 3]);
User::destroy(1, 2, 3);
In every cases, the number between brackets represents the object ID, but you may also run a delete query on a set of models:
$affectedRows = User::where('votes', '>', 100)->delete();
http://laravel.com/docs/5.0/eloquent#insert-update-delete
So the Laravel's way of deleting using the destroy function is
<form action="{{ url('employee' , $employee->id ) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete Employee</button>
</form>
You can find an example here http://laravel.com/docs/5.1/quickstart-intermediate#adding-the-delete-button
And your route should look something like this
Route::delete('employee/{id}', array('as' => 'employee.destroy','uses' => 'Employeecontroller#destroy'));
It works with eg:Route::resource('employee', 'EmployeeController'); and it should also work with how you set up your destroy route.
Obviously you have a bad routing problem. You're trying to use a 'get' verb to reach a route defined with a 'delete' verb.
If you want to use an anchor to delete a record, you should add this route:
Route::get('/employee/{id}/destroy', ['uses' => 'EmployeeController#destroy']);
or keep using a delete verb, but then you need to use a form (not an anchor) with a parameter called _method and value 'delete' stating that you're using a 'delete' verb.
Route::get('/showcon/{del_id}/delete','MainController#deletemsg');
public function deletemsg($del_id){
$mail=Mail::find($del_id);
$mail->delete($mail->id);
return redirect()->back();
}
del