I have a page, where when user pick a date, choose data from dropdown, and click search.
The page which initialy have 0 data in its table, will be populated with data.
The query to fetch the data is inside a controller class.
I have successfully ran the page before. But I didn't use any authentication (auth) feature.
And now, when I use laravel breeze as the authentication starter. I got an error because the program cannot found my controller class.
Here's the previous route (web.php) code, without auth class.
use App\Http\Controllers\GetEmployeePerformance;
Route::get('/performance', [GetEmployeePerformance::class, 'index']);
require __DIR__.'/auth.php';
Here's the current route (web.php) code
use App\Http\Controllers\GetEmployeePerformance;
Route::get('/performance', function () {
return view('performance', [GetEmployeePerformance::class, 'index']);
})->middleware(['auth', 'verified'])->name('performance');
require __DIR__.'/auth.php';
And here's the controller's code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class GetEmployeePerformance extends Controller
{
public function index()
{
$performance_summary = [];
$performance_detail = [];
if (request('date')){ ...}
return view('performance', compact('performance_summary','performance_detail'));
And this is how the value is called in view
<tbody>
#for ($x = 0; $x < count($performance_summary); $x++)
<tr>
...
This is error, the route failed to find the controller class.
Which parts should I fix?
I just found the fix on my own.
I changed the route code, into this code below
Route::get('/performance', [GetEmployeePerformance::class,'index'])->middleware(['auth', 'verified'])->name('performance');
I hope this answer can help other people who have similar problem as me :D
Related
I'm making a registration form where after filling in a form, users get an email with all the info they've submitted including a link where they can edit their registration. Example url: localhost/registrationapp/edit/{id}
I've been trying to pass part of the url to a controller, this is my route:
Route::get('/edit/{id}', [RegistrationappController::class, 'edit'])->with('id', $id);
And I got this function in my controller:
public function edit($id)
{
return 123;
$registration= Registrationapp::find($id);
return view('edit')->with('registration, $registration);
}
The return 123 part is just added to see if I can even get to the controller, but it doesn't reach the controller. Instead I'm getting this error when I go to a url (for example localhost/registrationapp/edit/5):
Undefined variable $id
Is there any way to do what I'm trying to do? Any help would be greatly appreciated.
Just remove ->with('id', $id);
Note : if you are using url : localhost/registrationapp/edit/{id} , make sure to include registrationapp in route :
Route::get('registrationapp/edit/{id}', [RegistrationappController::class, 'edit']);
I reckon you need to have your route just as Route::get('/registrationapp/edit/{id}', [TestController::class, 'edit']);.
Please feel free to look at my laravel 7 snippet here, https://phpsandbox.io/n/73314638-5fvdc. It demonstrates below:
Route::get('/registrationapp/edit/{id}', [TestController::class, 'edit']);
Note, I borrowed the default welcome.blade.php file, and updated the default / route in web.php.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function edit($id)
{
return view('welcome', ['id' => $id]);
}
}
Edit: You're right in your comment reply from accepted answer, I missed a prefix to your example URL you shared, apologies about that. I've updated my answer here.
I just moved a project from laravel 4.0 to laravel 5.2. Am using a fresh installation of laravel 5.2 as suggested by Taylor. i have successfully transfer all files and web app works fine.
My challenge is that i have a page called page.blade.php that stores some value in session {{ Session::put('item', $itemSelected) }} base on the user choice and i have a Session::get('item') in my controller to receive this and process some logic.
The session returns null in the controller but when i add {{Session::get('item')}} in to page.blade.php it display value stored in $itemSelected. I also observe that session created in controller method can be access by the page.blade.php but the session created by the page.blade.php can't be access by the controller method.
Am a little bit confuse here, i need help. This process worked fine in laravel 4.0 before i moved to laravel 5.2
Here is my route
route::group(['middleware' => ['web']], function () {
Route::get('page', 'Website\PageController#mypage');
});
Page.blade.php
'''''
$itemSelected= 'Page 1';
{{Session::put('item' , $itemSelected)}}
Page 1
......
My Controller
<?php
namespace App\Http\Controllers\Website;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Authuser;
use View;
use Redirect;
use Session;
return
class PageController extends Controller {
public function mypage()
{
...
$selectedPage = Session::get('item'); //this suppose to return "Page 1" but returns null
....
}
}
I can't call this an answer to this issue but another way to get the job done.
From the view page.blade.php, I pass the value $itemSelected as a $_GET variable on a url. When link is clicked the controller picks the value from the request with Input::get('item'). Then persist the return value from request into Session (Session::put('item',$itemSelected). This got the task done.
Because it hasn't been set yet, you set it in the view, which is created after you run that line. I am guessing that is what the '...' implies.
I have an installation of Laravel 5.1 and I want to share the route name with all my views. I need this for my navigation so I can highlight the corresponding navigation menu button depending on which page the user is on.
I have this code in my app\Providers\AppServiceProvider:
public function boot()
{
$path = Route::getCurrentRoute()->getName();
view()->share('current_route_name', $path);
}
and I am using this namespace:
use Illuminate\Support\Facades\Route;
but I am getting this error in my view:
Call to a member function getName() on a non-object
the interesting part is that if I write this in view it works with no problems at all:
{{ Route::getCurrentRoute()->getName() }}
Could anyone help me? am I not using the correct namespace or maybe it is not even possible to use Route at this point in the application?
Thank you!
you can use view share under view composer.
view()->composer('*', function($view)
{
$view->with('current_route_name',Route::getCurrentRoute()->getName());
});
Or
view()->composer('*', function($view)
{
view()->share('current_route_name',Route::getCurrentRoute()->getName());
})
I cloned this todstoychev/Laravel5Starter from Github and installed it.
After creating this StaticPagesController controller and updating my routes.php file. The controller does not seem to work. For some reason i keep getting the following error.
ReflectionException in ControllerInspector.php line 32:
Class App\Http\Controllers\StaticPagesController#faq does not exist
My routes.php file
<?php
// Admin routes
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
Route::controller('permissions', 'AdminPermissionsController');
Route::controller('settings', 'AdminSettingsController');
Route::controller('roles', 'AdminRolesController');
Route::controller('users', 'AdminUsersController');
Route::controller('/', 'AdminController');
});
// Public and user routes
Route::controller('contacts', 'ContactsController');
Route::controller('users', 'UsersController');
Route::controller('/', 'IndexController');
Route::controller('faq', 'StaticPagesController#faq');
My StaticPagesController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StaticPagesController extends Controller
{
public function faq(){
return 'this is faq page';
}
}
I have tried composer update, php artisan acl:update, composer dumpautoload to no avail.
Please help me. Thanks
With this line:
Route::controller('faq', 'StaticPagesController#faq');
You are telling Laravel that the controller for faq shoule be StaticPagesController#faq. The Route::controller method sets an entire controller for a route, it does not specify a method to be used on that route, Laravel handles this internally. Take a look at your error to prove my point:
Class App\Http\Controllers\StaticPagesController#faq does not exist
It is looking for class StaticPagesController#faq not StaticPagesController as you are intending.
Unless you are building an API using REST, you should not use the controller method and instead specify your routes explicitly, i.e.
Route::get('faq', 'StaticPagesController#faq');
This will use the faq method on your controller when the user makes a GET request to the URI faq. If you insist on using the controller method, then remove the #faq from the second argument and you will be good, although I'm pretty sure Laravel expects the methods index, show, create, etc to be in your controller. I suggest taking a look at the Laravel 5 Fundamentals video course to help you get a better understanding.
For reference I have been working with this tutorial https://scotch.io/tutorials/build-a-time-tracker-with-laravel-5-and-angularjs-part-2.
I wanted to become more familiar with laravel 5 since I had previously only used 4 and found the above tutorial which also mixed in a little angular js. I followed part one and two of the tutorials to the letter and set up a database using mysql and phpmyadmin like directed.
I get to a section about halfway through which sets up a group route with the prefix api to pull seeded data from the database and display it in the view.
// app/Http/routes.php
...
// A route group allows us to have a prefix, in this case api
Route::group(array('prefix' => 'api'), function()
{
Route::resource('time', 'TimeEntriesController');
Route::resource('users', 'UsersController');
});
After this point I go to the page and the area that was previously rendered with data from a file instead of a database is now blank. If I inspect the element I get "failed to load resource the server responded with a status of 404 (not found)" and it displays my path time-tracker-2/public/api/time.
The routes work with these two controllers to populate the page with userdata from my database
// app/Http/Controllers/TimeEntriesController.php
...
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\TimeEntry;
use Illuminate\Support\Facades\Request;
class TimeEntriesController extends Controller {
// Gets time entries and eager loads their associated users
public function index()
{
$time = TimeEntry::with('user')->get();
return $time;
}
// app/Http/Controllers/UsersController.php
...
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller {
// Gets all users in the users table and returns them
public function index()
{
$users = User::all();
return $users;
}
Again I haven't worked much with Laravel and this is my first time messing with angular so I don't know if I am missing something super obvious or what the deal is. I have checked over all my code and compared it to the sample code and they are identical other than my database information. I have also scrapped the project and started from scratch and still get the same error when I get to this point.
Any sort of direction to look would be greatly appreciated because this error is driving me nuts.
Remember to point on the public folder, not on the root folder. That's why your URL is time-tracker-2/public/api/time and not time-tracker-2/api/time. This should fix your 404 error.