Controller is returning a blank data - php

the controller is returning a blank data/view and I think something is wrong with my routes. if I remove {locale}, the data is retrieved.
Can anyone help with returning the data properly while my routes have {locale} in it? Here are my related code:
Web.php
Route::get('{locale}/projects/{id}/billings', 'ProjectController#showbilling')
->name('showbilling');
Route::post('{locale}/projects/{id}', 'ProjectController#addbilling')
->name('addbilling');
ProjectController.php
public function showbilling($id)
{
$billings = Project::find($id);
$locale = app()->getLocale();
return $billings;
//return view('admin.addbillings', compact('billings'));
}
Edit: Here's my full web.php
web.php
Route::get('/', function() {
return redirect(app()->getLocale());
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function () {
Route::get('/', function () {
return view('welcome');
})->name('main');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
//Customers
Route::get('/customers', 'CustomerController#showcust')->name('customers');
Route::post('/sendcust', 'CustomerController#sendcust')->name('sendcust');
//Items
Route::get('/items', 'ItemController#showitems')->name('items');
Route::post('/senditem', 'ItemController#senditem')->name('senditem');
//Projects
Route::get('/projects', 'ProjectController#showprojects')->name('projects');
Route::post('/sendproj', 'ProjectController#sendproj')->name('sendproj');
//ProjectBillings
Route::get('/projects/{id}/billings', 'ProjectController#showbilling')->name('showbilling');
Route::post('/projects/{id}', 'ProjectController#addbilling')->name('addbilling');
//Invoices
Route::get('/invoices', 'InvoiceController#showinvoice')->name('invoices');
Route::post('/sendinvoitem', 'InvoiceController#sendinvoitem')->name('sendinvoitem');
Route::get('/invoices/{id}/details', 'InvoiceController#showdetails');
Route::post('/updateitem','InvoiceController#updatedetail')->name('updateitem');
Route::get('invoices/{id}/generate', 'InvoiceController#generate');
Route::post('/updatestatus', 'InvoiceController#changestatus')->name('updatestatus');
});

You are passing 2 params in your route but accepting only 1 in the controller. Add locale.
public function showbilling($locale, $id)

Related

How can I fix laravel named routes to get the proper result

I'm a beginner with laravel and trying to figure out the routes. I searched the web but provided solutions don't help, so maybe you guys will be able to shed some light.
So below is my routes file (web.php)
Route::group(['middleware' => 'auth'], function () {
Route::get('/', [HomeController::class, 'home']);
Route::get('dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('billing', function () {
return view('billing');
})->name('billing');
Route::get('profile', function () {
return view('profile');
})->name('profile');
Route::get('rtl', function () {
return view('rtl');
})->name('rtl');
Route::get('user-management', function () {
return view('laravel-examples/user-management');
})->name('user-management');
Route::get('tables', function () {
return view('tables');
})->name('tables');
Route::get('virtual-reality', function () {
return view('virtual-reality');
})->name('virtual-reality');
Route::get('static-sign-in', function () {
return view('static-sign-in');
})->name('sign-in');
Route::get('static-sign-up', function () {
return view('static-sign-up');
})->name('sign-up');
Route::get('/logout', [SessionsController::class, 'destroy']);
Route::get('/user-profile', [InfoUserController::class, 'create']);
Route::post('/user-profile', [InfoUserController::class, 'store']);
Route::get('/login', function () {
return view('dashboard');
})->name('sign-up');
Route::get('/register', [RegisterController::class, 'create']);
Route::get('/user-list', [RegisterController::class, 'index'])->name('user-list');
Route::post('/register', [RegisterController::class, 'store']);
//Route::delete('user-destroy', [RegisterController::class, 'destroy'])->name('user-destroy');
Route::resource('users', RegisterController::class);
//Route::get('/viehicle-types', [ViehicleTypeController::class, 'index']);
// Route::resource('viehicle-types', ViehicleTypeController::class);
Route::group(['middleware' => ['role:admin|manager']], function () {
Route::resource('viehicle-types', ViehicleTypeController::class);
Route::resource('addon-types', AddonTypeController::class);
Route::resource('addons', AddonController::class);
Route::resource('job-types', JobTypeController::class);
Route::resource('jobs', JobController::class);
Route::resource('viehicles', ViehicleController::class);
Route::get('/finished-jobs/registerSelectJobType', [FinishedJobController::class, 'registerSelectJobType'])->name('xxxx');
Route::get('/finished-jobs/registerSelectJob/{jobTypeId}', [FinishedJobController::class, 'registerSelectJob'])->name('yyyy');
Route::resource('finished-jobs', FinishedJobController::class);
});
});
My problem is that some of the name routes won't work.
Mainly these 2 don't work and display a 404 Error:
Route::get('/finished-jobs/registerSelectJobType', [FinishedJobController::class, 'registerSelectJobType'])->name('xxxx');
Route::get('/finished-jobs/registerSelectJob/{jobTypeId}', [FinishedJobController::class, 'registerSelectJob'])->name('yyyy');
What is strange that when I check the routes:
$url=route('xxxx');
echo "The url is : " .$url;
I get the proper URL that works when entered in the web browser.
So I can't figure it out if anyone can help I will be greatful.
Cheers
HT

The POST method is not supported for this route. - Laravel

I have a problem with my create post page. When I submit I get this error:
The POST method is not supported for this route. Supported methods: GET, HEAD.
I have no clue where it comes from as I am pretty new to Laravel.
routes(web.php):
<?php
Route::get('/', [PagesController::class, 'index']);
Auth::routes();
Route::get('/blog', 'App\Http\Controllers\PostsController#index');
Route::get('/blog/create', 'App\Http\Controllers\PostsController#create');
Route::get('/logout', 'App\Http\Controllers\Auth\LoginController#logout', function (){
return abort(404);
});
Route::get('/home', 'App\Http\Controllers\HomeController#index');
Controller:
public function index()
{
return view('blog.index')
->with('posts', Post::orderBy('updated_at', 'DESC')->get());
}
public function create()
{
return view('blog.create');
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|mimes:jpg,png,jpeg|max:5048'
]);
$newImageName = uniqid() . '-' . $request->title . '.' .
$request->image->extension();
$request->image->move(public_path('images'), $newImageName);
Post::create([
'title' => $request->input('title'),
'description' => $request->input('description'),
'slug' => SlugService::createSlug(Post::class, 'slug',
$request->title),
'image_path' => $newImageName,
'user_id' => auth()->user()->id
]);
return redirect('/blog')
->with('message', 'Je post is toegevoegd!');
}
image for reference
You are using get for all your routes... You have to use Route::post... Read the documentation again about this topic so you can understand it better.
Remember that using Route::post, Route::get, Route::delete and more, is the method you are allowing the route to be accessed with.
It comes from your Routes:
Route::get('/', [PagesController::class, 'index']);
Auth::routes();
Route::get('/blog', 'App\Http\Controllers\PostsController#index');
Route::get('/blog/create', 'App\Http\Controllers\PostsController#create');
Route::get('/logout', 'App\Http\Controllers\Auth\LoginController#logout', function (){
return abort(404);
});
Route::get('/home', 'App\Http\Controllers\HomeController#index');
You have GET routes, but no POST routes. You need Route::post in order to support POST.
You syntax for registering the route for post method is incorrect.
It should be: Route::post($uri, $callback); where callback can be any function in controller layer or service layer.
If you want to register your route for multiple http verbs, you can do so using any of the below methods.
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
Refer to this documentation. It explains the concept in simple terms.

Laravel 5.6 distribute routes by user params

In the routes app/routes/web.php I have a group with a bunch of routes for resources:
Route::group(['middleware' => 'auth'], function()
{
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
});
Auth::routes();
Route::get('/logout', 'Auth\LoginController#logout')->name('logout');
And now I want to distribute routes in this group by user parameter:
use App\UserGroup;
$access = UserGroup::find( auth()->user()->group_id )->access;
Route::group(['middleware' => 'auth'], function()
{
if ($access == 1) {
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
}
});
But I can't get user params right in the Route::group because an error occurs: Trying to get property 'group_id' of non-object
How to get auth()->user()->group_id in app/routes/web.php before middleware?
Use $access param in callback
$access = UserGroup::find( auth()->user()->group_id )->access
Route::group(['middleware' => 'auth'], function() use ($access)
{
if ($access == 1) {
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
}
});
U can create middleware 'GroupAccess'. Then assign it to alias, i.e.
'groupAccess' => \App\Http\Middleware\GroupAccess::class
Finally in it's handle method check your condition
public function handle($request, Closure $next)
{
if (UserGroup::find( auth()->user()->group_id )->access === 1)
return $next($request);
else
return back();
}
Then just specify this middleware in route group
Route::group(['middleware' => ['auth','groupAccess']], function()
{
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
});

Laravel 5.4 routing for same prefix group but different middleware giving error

I need all the route under same prefix manager with one middleware for guest manager_guest and another for logged in user manager_auth.
This code bellow is my route web.php file.
Is there any other way ?
My routes:
Route::prefix('manager')->group(['middleware' => 'manager_guest'], function () {
Route::get('/register', 'Manager\RegisterController#showRegister')->name('manager.register.create');
Route::post('/register', 'Manager\RegisterController#register')->name('manager.register.store');
Route::get('/login', 'Manager\LoginController#showLogin')->name('manager.login.create');
Route::post('/login', 'Manager\LoginController#login')->name('manager.login');
});
Route::prefix('manager')->group(['middleware' => 'manager_auth'], function () {
Route::post('/logout', 'Manager\LoginController#logout')->name('manager.logout');
Route::get('/profile', 'Manager\PageController#profile')->name('manager.profile');
});
Error after executing php artisan route:list
PHP Warning: Uncaught ErrorException: Array to string conversion in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php:329
Stack trace:
#0 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Array to string...', 'E:\\laragon\\www\\...', 3
29, Array)
#1 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): require()
#2 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(285): Illuminate\Routing\Router->loadRoutes(Array)
#3 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\RouteRegistrar.php(104): Illuminate\Routing\Router->group(Array, Array)
#4 E:\laragon\www\laraveladmin\routes\web.php(30): Illuminate\Routing\RouteRegistrar->group(Array, Object(Closure))
#5 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): require('E:\\laragon\\www\\...')
#6 in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php on line 329
PHP Fatal error:  Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='E:\Developer\Wbserver\php\PEAR') in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate
\Routing\Router.php on line 329
 
[Symfony\Component\Debug\Exception\FatalErrorException] Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='E:\Developer\Wbserver\php\PEAR')
Try this instead
Route::group(['prefix' => 'manager', 'middleware' => 'manager_guest'], function() {
});
You could "factorize" your code like this:
Route::prefix('manager')->group(function () {
Route::middleware(['manager_guest'])->group(function () {
// These will be prefixed with "manager" and assigned the "manager_guest" middleware
});
Route::middleware(['manager_auth'])->group(function () {
// These will be prefixed with "manager" and assigned the "manager_auth" middleware
});
// These will just be prefixed with "manager"
});
I noticed all your controllers live in the sub-namespace Manager. You can chain the methods and make your routes file even cleaner. For instance:
Route::prefix('manager')->namespace('Manager')->group(function () {
Route::middleware(['manager_guest'])->group(function () {
Route::get('register', 'RegisterController#showRegister')->name('mananger.register.create');
});
Route::middleware(['manager_auth'])->group(function () {
Route::get('profile', 'PageController#profile')->name('mananger.profile');
});
});
None of the other answers worked for me as I had a lot of routes to change, and didn't want to change namespaces. The key to making this work is "as". The downside of this being that it changes the path when using "route()", but your use of name on each route here would override that anyway.
Route::group(['prefix' => 'manager', 'middleware' => ['manager_guest'], 'as' => 'manager_guest'], function() {
...
}
Route::group(['prefix' => 'manager', 'middleware' => ['manager_auth'], 'as' => 'manager_auth'], function() {
...
}

Laravel 5.4 : Translate routes

is there a simple way to translate my routes in Laravel 5.4. My translation files located in here:
/resources
/lang
/en
routes.php
/de
routes.php
In my web.php i define my routes like this:
Route::get('/{locale}', function ($locale) {
App::setLocale($locale);
return view('welcome');
});
Route::get('{locale}/contact', 'ContactController#index');
I have found very elaborate solutions or solutions for Laravel 4. I am sure that Laravel has also provided a simple solution. Can someone explain to me the best approach?
Thanks.
we usually do it like this
to get the current language:
$request = request()->segment(1);
$language = null;
if (!empty($request) && in_array($request,config('translatable.locales'))) {
$language = $request;
App::setLocale($language);
} else {
$language = 'nl';
}
routes:
Route::group(['prefix' => $language], function () {
Route::get(trans('routes.newsletter'), array('as' => 'newsletter.index', 'uses' => 'NewsletterController#index'));
I created a file translatable.php in my config folder:
<?php
return [
'locales' => ['en', 'de'],
];
web.php:
$request = request()->segment(1);
$language = null;
if (!empty($request) && in_array($request,config('translatable.locales'))) {
$language = $request;
App::setLocale($language);
} else {
$language = 'de';
}
Route::get('/', function() {
return redirect()->action('WelcomeController#index');
});
Route::group(['prefix' => $language], function () {
/*
Route::get('/', function(){
return View::make('welcome');
});
*/
Route::get('/',
array( 'as' => 'welcome.index',
'uses' => 'WelcomeController#index'));
Route::get(trans('routes.contact'),
array('as' => 'contact.index',
'uses' => 'ContactController#index'));
});
Works fine - Thanks. Is the redirect also the best practice?
Best regards

Categories