Laravel Action Route not Define - php

I have unique problem. I already create Route on web.php, on Controller, and Blade. and working on my localhost. but after publish to real server, I jus got error Action url\controller#function not define. this is my code.
Route::get('/timetableschedule', 'ManagementController#EmployeeTimeTable');
this is my web.php
public function EmployeeTimeTable(Request $request){
$session = $request->session()->get('user.id');
$companysession = $request->session()->get('user.location');
$locationdata = DB::table('company')
->join('companyrole','companyrole.company_id','=','company.Company_id')
->join('users','users.id','=','companyrole.user_id')
->where('users.id',$session)
->get();
$userlist = db::table('users')
->join('companyrole','users.id','companyrole.user_id')
->where('companyrole.company_id',$companysession)
->whereNotNull('users.NPK')
->select('users.id','users.name','users.NPK')
->orderby('users.name')
->get();
$timesetting = db::table('time_leaving')
->where('company_id',$companysession)
->get();
$leaveschedule = db::table('employee_leaving')
->join('users','employee_leaving.user_id','users.id')
->where('employee_leaving.company_id',$companysession)
->select('employee_leaving.leaving_date','users.name')
->get();
return view('Management.employeetimetable',['location'=>$locationdata,'UserList'=>$userlist,'ListTimeSet'=>$timesetting,'LeavingSchedule'=>$leaveschedule]);
}
this is my controller
<li>
<a href="{{action('ManagementController#EmployeeTimeTable')}}" class="dropdown-item">
<p>Employee Timetable</p>
</a>
</li>
and this is code on blade to call controller#function
this step same as another controller, web, and blade.php. but, just for this rout get me a trouble. thank you

You have a problem with your route cache, simply do this:
php artisan cache:clear
php artisan route:cache
if you want to do same on the server you can define a route in web.php for that like this:
Route::get('/clear/route', 'ConfigController#clearRoute');
and make ConfigController.php like this
class ConfigController extends Controller
{
public function clearRoute()
{
\Artisan::call('route:clear');
}
}
and then go to that route on server example http://your-domain/clear/route

If your controller is on a subfolder, and not in app\Http\Controllers you will need to provide the fully qualified class name of the controller to the action helper :
action('App\Http\Controllers\Admin\ManagementController#EmployeeTimeTable')
by default action helper will look for : App\Http\Controllers\ManagementController
note that this syntax will not work as you might expect :
action('Admin\ManagementController#EmployeeTimeTable')
because laravel will not add App\Http\Controllers if there is a \ in the action name parameter

Related

Laravel 8 Route not defined even route is defined

This error is comeup while i working on laravel 8
Route [product.details] not defined. (View: C:\xampp\htdocs\ecommerce\resources\views\livewire\shop-component.blade.php)
here is the route i made Route Page web.php
Route::get('/product/{slug}',DetailsComponent::class)->name('product.details');
this is line of view where i want that route View Page shop-component.balde.php
<a href="{{route('product.details',['slug'=>$product->slug]) }}" title="{{$product->name}}">
and this one is the Details Component code
namespace App\Http\Livewire;
use Livewire\Component;
class DetailsComponent extends Component
{
public $slug;
Public function mount($slug){
$this->slug = $slug;
}
public function render()
{
$product = Product::where('slug', $this->slug)->first();
return view('livewire.details-component',['product'=>$product])->layout('layouts.base');
}
}
I think this php code doesnot get route of product.details at the same time i create middleware file and define a different route in it this route is working in this page but not Product.details.
Kindly help me i am stuck from 1 week here
You can use
php artisan route:list
to show a list of registered routes - make sure it shows up there. If it doesn't, use
php artisan route:clear
to clear Laravel's route cache
It does not work because of your route. It's not write in the correct way. You have first to make your DetailsComponent::class is bracket then you have to give the name of the method you want to use in this class.
Route::get('/product/{slug}', [DetailsComponent::class, 'method name'])->name('product.details');

Route [course] not defined

I created a resource controller. The problem is when I code a route in my view, the browser displays Route [course] not defined.
I run the php artisan route:list command and I realize that the route does not exist in the route list.
The controller method
public function index()
{
$courses = Course::where('user_id', Auth::user()->id)->paginate(10);
return view('teacher.teachercourse.courses', compact('courses'));
}
The web.php code
Route::get('course', 'CoursesController#index')->name('course');
Route::get('course', 'CoursesController#create')->name('course.create');
The link
<li>Courses</li>
As both routes are get you can't use the same uri:
change the uri example:
Route::get('courses', 'CoursesController#index')->name('courses.index');
Route::post('course/create', 'CoursesController#create')->name('course.create');

I am unable to access Laravel view

I have just created one of about view in view/about.blade.php, and I am accessing this from localhost/myproject/public/about, but it's not working.
However, localhost/myprojects/public/ is working fine; about view has been created on same parameters as welcome by default in Laravel.
Firstly the information is not sufficient to say anything.Please provide your route.Also its important how you are running your project ,is it via Xampp(or Lampp whatever is there) or "php artisan serve"
but looking from your working directory "localhost/myprojects/public" I guess its not by the command . Try localhost/myprojects/public/about.blade.php or run it by php artisan serve and try route localhost:8000/about
Have you added particular routing to web.php file?
Route::get('about', function () {
return view('about');
});
https://laravel.com/docs/5.7/routing
Which error are you getting?
404 - Not found
Route::get('/about', function () {
return view('about');
});
Check routes
php artisan route:list
Laravel is a MVC Framework, Which means You Have a Controller which procede some logic when some request come in and interact with the model if need, after that the controller return some view.
And because you whan to acccess your view file, you must past through controller, and that controller will render the view. Because the views folder is not in the \public dicretory as subdirectory you can't access to It with url like localhost/myproject/public/about even if you get access to it, you will not get HTML, you'll get some plain text with Blade tags. It's a must to return view in you controller by rendering it, somewhere in the background Laravel procede all Blade Tag and return HTML that correspond to that tags.
What I can suggest you Is to create some route in your route file like this
Route::get('/about', function(Request $request){
// Automatically Laravel will look this file in the view directory
return view('about');
});
Or you can go with the controller like procedure by creating some controller, go in your terminal and execute
php artisan make:controller AboutController
this will generate a file name AboutController.php in app\Http\Controllers diretory within witch you will found
namespace App\Http\Controllers;
class HomeController extends Controller
{
}
after that add
public function index()
{
return View::make('about');
}
Don't forget to include use the Illuminale\Supports\Facades\View on top of your file
And one more important thing which left is to configure the Route, for that go in routes directory in the web.php file add
Route::get('/about', 'AboutController#index')->name('about');

Showing wrong view in laravel 5.4

I am using laravel 5.4 and trying to get index page, i am using following routes
Route::get('/',
['as' => 'home_page',
'uses' => 'Controller#index']);
and index function in controller looks like this:
public function index()
{
return view('index');
}
But when I visit mydomain.com, I get a different view than index.blade.php.
and it is fine when I use mydomain.com/? or on my local server.
I have searched everywhere in my code and in a google, but didn't found anything, any help?
ie: let me know if any further information required.
First make sure you are calling the right controller, and this dont have a specific middleware blocking the acess to your index method and index.blade.php is inside view folder.
If all of this is fine try this code on your rotes file:
Route::get('', function () {
return view('index');
})
Try this.
First use the make:controller Artisan command to create a controller file. let's say it is homeController.
php artisan make:controller homeController
Then in the homeController file write your code to get the view.
<?php
namespace App\Http\Controllers;
class homeController extends Controller
{
public function index()
{
return view('index');
}
}
Then define a route to this controller.
Route::get('/', 'homeController#index');
For more information please refer https://laravel.com/docs/5.5/controllers
There was a cached view saved on my server, I used
php artisan cache:clear and it got fixed. Thank you everyone for the support.

Laravel 5.5 Resource Controller and Dependecy Injection

I am working on a Laravel 5.5 application. When I use php artisan make:model SomeModel -mr it creates the model, migration and resource controller.
I've been noticed that some methods have by default only one parameter: the model:
public function show(SomeModel $someModel)
{
...
}
If you look into the $someModel variable it has an empty SomeModel object.
I was reading on Laravel Documentation that it looks like the Containers or Facades but I am not sure how to use this. Do you?
Edit 1:
I had my routes defined in routes/web.php as: Route::resource('users', 'UserController');
Now I had to define all the routes manually since automatic binding was not working:
Route::get('users', 'UserController#index');
Route::get('users/create', 'UserController#create');
Route::post('users', 'UserController#store');
Route::get('users/{user}/edit', 'UserController#edit', function(App\User $user) {});
Route::post('users/{user}', 'UserController#update', function(App\User $user) {});
Route::post('users/{user}/delete', 'UserController#destroy', function(App\User $user) {});
So, should I replace every resource controller route to manual routing like this?
The resource controller is expecting you to use route model binding. In your routes file, each route that corresponds to a controller action with an injected model will need to have a matching parameter.
For example:
Route::get('user/{user}', 'UserController#show');
Using the above route, the following controller action would receive a user instances that corresponds to the user ID passed as a URL parameter.
class UserController extends Controller
{
public function show(User $user)
{
...
}
}
The reason you're seeing an empty model now is that Laravel will just pass and fresh model to the controller if it is not bound to a route parameter. In other words, if you forget to bind the model in your routes file automatic injection will just give you a new instance.
Note that if you are using a route resource the resulting routes should already have the correct parameters
Route::resource('users', 'UserController');
You can run php artisan route:list to confirm that your actual routes are correct.
Your problem is your controller is expecting two parameters like below:
public function show($id, User $user)
if you try:
public function show(User $user)
it should work correctly.
In your route you are passing only a single param like:
user/{user}
So if you dd the first param it will display the number 1 but if you pass that
to the model it will return the corresponding user as per what id you pass in the route.
The reason your User model was returning an empty object was because there was no value passed to it.
Also make sure your route placeholder: /{user} matches the variable name in
the controller: public function show(User $user).
Hope this helps.
I too came across with the same problem.
If your model having two or more words, you have to use only small letters like $modeModel as $somemodel.
public function show(SomeModel $somemodel)
{
...
}

Categories