Laravel Route Url - Query String - Multi Level - php

I am trying to achieve url structure like below.
example.com/clients/{client_id} //done
example.com/clients/{client_id}{project_id} // issues
Error is Missing required parameters for [Route: clients.show_project] [URI: clients/{client}/{project_id}].
Route::group(['prefix' => 'clients', 'as' => 'clients.'], function () {
Route::get('/', [
'uses' => 'ClientsController#index',
'as' => 'index',
]);
Route::get('/create', [
'uses' => 'ClientsController#create',
'as' => 'create',
]);
Route::post('/store', [
'uses' => 'ClientsController#store',
'as' => 'store',
]);
Route::group(['prefix' => '{client}', '__rp' => ['menu' => 'clients']], function () {
Route::get('/', [
'uses' => 'ClientsController#show_client',
'as' => 'show',
]);
});
Route::group(['prefix' => '{client}/{project_id}'], function () {
Route::get('/', [
'uses' => 'ClientsController#show_project',
'as' => 'show_project',
]);
});
});
On view
{{ $task->title }}
Controller
public function show_project($client, $project_id)
{
$project_threads = Project_Threads::where('project_id', $project_id)->get();
return $project_threads;
}

The problem is in your view. Your have to pass params in an array in route(). Try this:
{{ $task->title }}

Related

Laravel 5.4 double arrow error in routes (syntax error, unexpected '=>' (T_DOUBLE_ARROW))

I used this routes for either Laravel 5.1 and Laravel 5.3, and now when I'm using this type of route order it gives me the title error hope you can help me, you can find the code here :
Route::prefix('productos')->group(function () {
'as' => 'products.index',
'uses' => 'ProductController#index'
Route::get('crear',[
'as' => 'products.create',
'uses' => 'ProductController#create'
]);
Route::post('guardar',[
'as' => 'products.store',
'uses' => 'ProductController#store'
]);
// Editar, borrar
Route::get('{id}',[
'as' => 'products.destroy',
'uses' => 'ProductController#destroy'
]);
Route::get('{id}/editar',[
'as' => 'products.edit',
'uses' => 'ProductController#edit'
]);
Route::put('{id}',[
'as' => 'products.update',
'uses' => 'ProductController#update'
]);
});
To use => you need to be in the context of an associative array in php. In your case you are using it inside a closure:
Route::prefix('productos')->group(function () {
// This section is incorrect
'as' => 'products.index',
'uses' => 'ProductController#index'
// Because is not inside an array
Route::get('crear',[
'as' => 'products.create',
'uses' => 'ProductController#create'
]);
...
If I had to guess what you are looking for something like this:
Instead of
'as' => 'products.index',
'uses' => 'ProductController#index'
You should have something like:
Route::get('listar',[
'as' => 'products.index',
'uses' => 'ProductController#index'
]);
So the endpoint would be productos/listar.
Hope this helps you.
Syntax error
'as' => 'products.index',
'uses' => 'ProductController#index'
Change it like this
Route::get('products',[
'as' => 'products.index',
'uses' => 'ProductController#index'
]);

Route not displaying the correct page

I've got 2 sections to my site, the admin side and the public side. The issue I'm having is that if I go to for example admin/menus then I go to my public side instead of going to the menus page.
I'm not sure why this is happening. I've tried to re-arrange the order of the routes in my public side but that didn't work and I've drawn a blank as to what I've done wrong.
My public routes
<?php
Route::get('/', [
'uses' => 'OpenController#index',
'as' => 'index',
]);
Route::get('/{id}', 'OpenController#content');
Route::post('/contact', [
'uses' => 'OpenController#contact',
'as' => 'contact',
]);
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
Route::any('/search', [
'uses' => 'OpenController#search',
'as' => 'search'
]);
my admin menus route
Route::resource('admin/menus', 'MenusController');
My productItem function
public function productItem($category, $slug)
{
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$contact = Contact::all();
$single_product = Product::where('slug', $slug)->get();
return view('open::public.single_item', compact('menus_child', 'contact', 'single_product'));
}
The error come in with this route
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
If I remove this route then it works, but I need this route so I can't remove it.
If I'm missing something else that I need to give please let me know.
This will work if u put it at the top, but it may clash with other routes i think.
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
you can try like this if u want
Route::get('/category/{category}/{slug}', function (\Illuminate\Http\Request $request) {
echo "ok";
});
You should prefix the route /{category}/{slug} to avoid conflicts. So replace:
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
By:
Route::get('/open/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
And update your links to that route in your views.
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
Will catch every combination of path that have two items /admin/menus, /admin/anything or /foo/bar. You are probably going to run into the same problem with
Route::get('/{id}', 'OpenController#content');
If you can not rename your routes, you need to put all the more restrictive routes on top and your less restrictive routes on the bottom.
Route::resource('admin/menus', 'MenusController');
Route::get('/', [
'uses' => 'OpenController#index',
'as' => 'index',
]);
Route::post('/contact', [
'uses' => 'OpenController#contact',
'as' => 'contact',
]);
Route::any('/search', [
'uses' => 'OpenController#search',
'as' => 'search'
]);
Route::get('/{category}/{slug}', [
'uses' => 'OpenController#productItem',
'as' => 'product.item',
]);
Route::get('/{id}', 'OpenController#content');
UPDATE
You have a few options.
Here are two of them.
You can limit what the route will accept with RegEx.
See Route Parameters > Regular Expression Constraints
Route::get('/{category}/{slug}', function () {
return 'hello';
})->where('category', '[one]*[two]*[three]*[four]*[five]*');
Or you can change the caffeine route through its config.
php artisan vendor:publish --tag=genealabs-laravel-caffeine
Then change the route in /app/config/genealabs-laravel-caffeine.php
There are some other ways as well. I'd just up a quick test site and start messing with routes to see what works the best for your needs.

Middleware auth not working with web Laravel 5.2

Hi I am having some problem with authentication in laravel. I have to use two middleware 1. is web and 2. auth . I am using web middleware so that I can use session to show flash messages. and want to use auth middleware to do authentication of users/admin. but I am facing some problems.
below is my function to check authorization and to redirect to their respective routes
public function postLoginForm(){
$email=Input::get('email');
$password=Input::get('password');
$data=[
'email'=>$email,
'password'=>$password
];
$rules=[
'email'=>'required',
'password'=>'required'
];
$validator=Validator::make($data,$rules);
if($validator->fails()){
Session::flash('fail', 'Oops Something went wrong!!');
return redirect()->back()->withErrors($validator);
}
else{
if(Auth::attempt($data)){
$checkStatus=User::select('*')->where('email',$email)->first();
Session::put('email',$checkStatus->email);
Session::put('user_type',$checkStatus->user_type);
if($checkStatus['user_type']=='4'){
if($checkStatus['status']=='0'){
Session::flash('wait', 'Registration is not approved!!');
return "student";
return redirect()->back();
}
else{
return "student else";
return Redirect::route('get.student.dashBoard');
}
}
else if($checkStatus['user_type']=='1'){
return Redirect::route('get.admin.dashBoard');
}
else if($checkStatus['user_type']=='2'){
return 'admin sir view';
return Redirect::route('get.admin.dashBoard');
}
else if($checkStatus['user_type']=='3'){
return 'admin other view';
return Redirect::route('get.admin.dashBoard');
}
else{
Session::flash('fail', 'Oops Something went wrong!!');
return redirect()->back();
}
}
else{
Session::flash('fail', 'Login details not matched!!');
return redirect()->back();
}
}
return 'nothing works';
}
below is my routes for admin
Route::group(['middleware' => ['web']], function () {
Route::get('/login',
['as' => 'get.login.page',
'uses' => 'LoginController#getLoginPage']);
Route::post('/login-done',
['as' => 'post.login.page',
'uses' => 'LoginController#postLoginForm']);
Route::get('/register',
['as' => 'get.register.page',
'uses' => 'LoginController#getRegisterPage']);
Route::post('/register',
['as' => 'post.register.form',
'uses' => 'LoginController#postRegisterForm']);
Route::get('/forgot-password',
['as' => 'get.forgotPassword.form',
'uses' => 'LoginController#getForgotPasswordForm']);
Route::group(['middleware' => ['auth']], function () {
Route::get('/admin-dashboard',
['as' => 'get.admin.dashBoard',
'uses' => 'admin\PageController#getAdminDashboard']);
Route::get('/all-achievements',
['as' => 'get.achievements',
'uses' => 'admin\AchievementsController#getAchievementsList']);
Route::get('/new-achievement',
['as' => 'get.add.achievement',
'uses' => 'admin\AchievementsController#getAddAchievement']);
Route::post('/add-achievement',
['as' => 'post.achievementsForm',
'uses' => 'admin\AchievementsController#postAchievements']);
Route::get('remove-achievement/{achie_slug}',
['as' => 'post.delete.achievements',
'uses' => 'admin\AchievementsController#postDeleteAchievement']);
Route::get('edit-achievement/{achie_slug}',
['as' => 'get.edit.achievements',
'uses' => 'admin\AchievementsController#getEditAchievement']);
Route::post('update-achievement/{ach_id}',
['as' => 'post.edited.achievement',
'uses' => 'admin\AchievementsController#postEditedAchievement']);
Route::get('/all-news',
['as' => 'get.news.list',
'uses' => 'admin\NewsController#getNewsList']);
Route::get('/add-news',
['as' => 'get.add.news',
'uses' => 'admin\NewsController#getAddNews']);
Route::post('/add-news',
['as' => 'post.add.news',
'uses' => 'admin\NewsController#postAddNews']);
Route::get('/delete-news/{news_slug}',
['as' => 'get.delete.news',
'uses' => 'admin\NewsController#postDeleteNews']);
Route::get('/edit-news/{news_slug}',
['as' => 'get.edit.news',
'uses' => 'admin\NewsController#getEditNews']);
Route::post('/edit-news/{news_slug}',
['as' => 'post.edited.news',
'uses' => 'admin\NewsController#postEditedNews']);
Route::get('/all-admins',
['as' => 'get.admin.list',
'uses' => 'admin\AdminController#getAllAdminList']);
Route::get('/add-admin',
['as' => 'add.new.admin',
'uses' => 'admin\AdminController#getAddNewAdmin']);
Route::post('/add-new-admin',
['as' => 'post.add.new.admin',
'uses' => 'admin\AdminController#postAddNewAdmin']);
Route::get('/all-schedule',
['as' => 'get.timeTable.list',
'uses' => 'admin\TimeTableController#getTimeTableList']);
Route::get('/add-schedule/{id}',
['as' => 'add.timeTable',
'uses' => 'admin\TimeTableController#getAddNewBatch']);
Route::post('/add-new-batch',
['as' => 'add.newBatch',
'uses' => 'admin\TimeTableController#postAddNewBatch']);
Route::post('/save-year-batch',
['as' => 'save.year.batch',
'uses' => 'admin\TimeTableController#postSaveYearBatch']);
Route::get('/schedule-table/{year}',
['as' => 'view.schedule.table',
'uses' => 'admin\TimeTableController#getScheduleTable']);
Route::get('/delete-schedule/{slug}',
['as' => 'delete.schedule.one',
'uses' => 'admin\TimeTableController#postDeleteOneSchedule']);
Route::get('/edit-schedule/{slug}',
['as' => 'edit.schedule.one',
'uses' => 'admin\TimeTableController#getEditScheduleForm']);
Route::post('/save-edited-schedule/{id}',
['as' => 'save.edited.schedule',
'uses' => 'admin\TimeTableController#postEditScheduleForm']);
Route::get('/all-results',
['as' => 'get.all.results',
'uses' => 'admin\ResultsController#getAllResults']);
Route::get('/add-result',
['as' => 'get.add.results',
'uses' => 'admin\ResultsController#getAddResult']);
Route::post('/add-new-result',
['as' => 'post.add.result',
'uses' => 'admin\ResultsController#postAddResult']);
Route::get('/delete-result/{id}',
['as' => 'get.delete.student.result',
'uses' => 'admin\ResultsController#getDeleteResult']);
Route::get('/edit-result/{id}',
['as' => 'get.edit.student.result',
'uses' => 'admin\ResultsController#getEditResult']);
Route::post('/save-edited-result/{id}',
['as' => 'post.edited.result',
'uses' => 'admin\ResultsController#postEditedResult']);
Route::get('/contact-messages',
['as' => 'get.contact.message',
'uses' => 'admin\ContactMessageController#getAllContactMessages']);
Route::get('/contact-messages/{id}',
['as' => 'get.delete.contact.message',
'uses' => 'admin\ContactMessageController#getDeleteContactMessages']);
});
});
every time i try to login it redirects me to the same login page. please guide me whats wrong with this.
You should remove web middleware from middleware group to make it work. It applies to all routes inside web.php (5.3) and routes.php (5.2.27 and higher) automatically and if you'll add it manually, it will break session related functionality.

ReflectionException in Route.php line 335: Function () does not exist

I'm tryin to get this grid (http://www.mariogallegos.com/tutorials/crud-custom-form) to work in Laravel 5.3.19.
I'm getting the exception:
ReflectionException in Route.php line 335: Function () does not exist
In my web.php i have the followingcode:
Route::group(['middleware' => 'sidebarmenu'], function()
{
Route::get('/home', [
'as' => 'home',
'uses' => 'HomeController#index'
]);
Route::get('/users', [
'as' => 'users',
GridEncoder::encodeRequestedData(new UserRepository(new User()), Request::all())
]);
});
You need to wrap your controller code with a function callback.
Replace
Route::get('/users', [
'as' => 'users',
GridEncoder::encodeRequestedData(new UserRepository(new User()), Request::all());
]);
with
Route::get('/users', function() {
GridEncoder::encodeRequestedData(new UserRepository(new User()), Request::all());
})->name('users');

Laravel Route group issue

I have been using laravel for a while now, but I stumbled across an error which I have never encountered before. It is probably me overlooking it, but with the route file given below, the route group with the prefix account gives a blank page. When going to /account/anunregisteredroute it does give a httpnotfoundexception
My routes.php file:
http://pastebin.com/EnnGSm10
By adding a / before the parameter you can solve this issue:
Route::get('/{username}', ['as' => 'account-profile', 'uses' => 'AccountController#getProfile']);
This piece of code worked for me:
Route::group(['prefix' => 'account'], function () {
Route::get('/{username}', ['as' => 'account-profile', 'uses' => function($username){
echo $username;
}]);
Route::get('profile', ['as' => 'account-edit-profile', 'uses' => 'AccountController#getUpdate', 'before' => 'auth']);
Route::post('profile', ['as' => 'account-edit-profile', 'uses' => 'AccountController#postUpdate', 'before' => 'auth|csrf']);
Route::group(['before' => 'guest'], function () {
Route::get('create', ['as' => 'account-create', 'uses' => 'AccountController#getCreate']);
Route::get('signin', ['as' => 'account-signin', 'uses' => 'AccountController#getSignin']);
Route::group(['before' => 'csrf'], function() {
Route::post('create', ['as' => 'account-create', 'uses' => 'AccountController#postCreate']);
Route::post('signin', ['as' => 'account-signin', 'uses' => 'AccountController#postSignin']);
});
});
});
I got the expected output.

Categories