I have this route:
Route::get('/', function () {
return view('index');
});
Route::resource('admin', 'EmployeeController');
I have model Employee and EmployeeController( with empty resource methods)
Error : Call to undefined method Illuminate\Routing\ResourceRegistrar::addResourceEmployee()
What is wrong with my code? I have used the same approach in other project and it worked.
Route::resource('admin', 'EmployeeController');
is attempting to bind to a model named Admin.
Route::resource('employees', 'EmployeeController');
should work with the model you have. To make it work with admin, name the resource parameter.
Route::resource('admin', 'EmployeeController', ['parameters' => [
'admin' => 'employee'
]]);
edit
Did you reference something outside the Laravel docs to use AddResourceEmployee(). Seems like a custom solution to me.
https://stackoverflow.com/a/16661564/320487
Related
is there a way to use the new automatic scoping in resource routes? Everything I tried didn't work:
Route::apiResource('instances/{instance:id}/projects', 'ProjectController', [
'except' => ['destroy']
]);
The following manual solution is working but would be a mess in the routes file.
Route::get('instances/{instance:id}/project/{project:id}',function(Instance $instance, Project $project){
return response()->json($project);
});
Thanks
I had a similar problem and as far as I can tell, the custom route key needs to be specified on the nested resource to trigger automatic scoping. The easiest way to do it without specifying each route separately is probably using parameters():
Route::apiResource('instances.projects', 'ProjectController')->parameters([
'projects' => 'project:id'
]);
it is probably help you
Route::group(['prefix' => 'instances/{instance:id}'], function () {
Route::apiResource('project', 'ProjectController', [
'except' => ['destroy']
]);
});
it could be an issue your models are in some other directory. you have to resolve the models first to be injected.
public function boot()
{
parent::boot();
Route::model('instance', App\Instance::class);
Route::model('project', App\Project::class);
}
You have to explicitly bind the models to keys.
Route::get('instances/{instance:id}/project/{project:id}',function(Instance $instance, Project $project){
return response()->json($project);
});
im learning laravel so if im not well as you are accept my apologies...
my problem is when i try to define a new method in web.php i got error!some times phpstorm sets problem on 'Route'word so i can run my blade pages but sometimes it sets problem on 'get','post','group' ,...and i cant run my app
ill show you how i defined my routes
use Illuminate\Routing\Route;
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'admin'] ,function (){
Route::get('/users','UsersController#index');
});
after all this my point is making a controller so i got this error to fix so i can move on...
Named groups in laravel
Route::group(['prefix'=>'admins','as'=>'admin.'], function(){
Route::get('users', ['as' => 'user', 'uses' = > 'UsersController#index']);
});
Also make sure you have index method in your UsersController.
FYR :- https://laraveldaily.com/laravel-5-1-names-for-route-groups/
In previous versions of Laravel I was using something like this in controller in show function
Route::resource( 'our-project', 'ProjectController' );
public function show( Project $project ) {
return view( 'portalComponents.projects.projectDetails', compact( 'project' ) );
}
I was trying the same in laravel 5.8 but the $project attributes comes empty.
Route model binding won't work for our-project/1 because laravel can't infer the model. It tries to bind the our-project placeholder to a variable that has the name name in the show method. That argument doesn't exist. Because if this the $project variable stays empty.
the following resource would work:
Route::resource( 'projects', 'ProjectController' );
because this uses the project placeholder in routes. Check the output from php artisan route:list
It is also possible to have the same resource with different prefixes:
Route::resource('projects', 'ProjectController');
Route::group(['prefix' => 'admin'], function () {
Route::resource('projects', 'ProjectController');
});
the first one is /projects/1 and the second one is /admin/projects/
For the sake of completness and as alternative to #MaartenDev right answer, if you want to define the name of the parameter used with a resource route you can use the parameters() function, i.e.:
Route::resource( 'our-project', 'ProjectController' )
->parameters(['our-project' => 'project']);
I'm developing an application with Laravel 5.8. In my application, I have a controller that handles backend articles, and it works. I want to display my user-side information in such a way that a user can click on a link and see the detail of an article. For that, I have created a new controller a with a new namespace for the function show my redirection of navigation in different page does not focus that it is en route or URL with Laravel 5.8. Below is the function.
namespace App\Http\Controllers\Cybernaut;
use App\History;
use App\Http\Controllers\Controller;
class HistoryController extends Controller
{
public function show($id)
{
$history = History::find($id);
return view('show_history', compact('history'));
}
}
At the level of the home page I wanted to have my links like these:
<li><a data-hover="history" href="{{route('history.show',$history→id)}}"><span>history</span></a></li>
Error
ErrorException (E_ERROR) Property [id] does not exist on this
collection instance. (View:
C:\laragon\www\venome\resources\views\layouts\partial\header.blade.php)
And here is the route used for the show function.
Route::group(['namespace'=>'cybernaut'], function (){
Route::get('/history/{slug}','HistoryController#show')->name('history.show');
});
Try after modifying the thing I have these at the route level now.
Route::get('/', 'FrontController#index')->name('index');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin/dashboard', 'DashboardController#index')->name('admin.dashboard');
Route::group([], function () {
Route::get('/history', 'HistoryController#index')->name('history.index');
Route::get('/history', 'HistoryController#create')->name('history.create');
Route::get('/history/edit', 'HistoryController#update')->name('history.update');
Route::get('/history', 'HistoryController#destroy')->name('history.destroy');
});
Route::group(['namespace' => 'cybernaut'], function () {
Route::get('/history/{history}', [
'as' => 'show',
'uses' => 'HistoryController#show'
]);
});
At the level of the homepage I wanted to put my link like those here now;
#foreach($history as $history)
<li><a data-hover="history" href="{{url('/history/'.$history->id)}}"><span>history</span></a></li>
#endforeach
I have this error now:
Trying to get property 'id' of non-object (View:
C:\laragon\www\venome\resources\views\layouts\partial\header.blade.php)
I want an internaut to be able to navigate between the pages.
You have a conflict of variables on your homepage.
#foreach($history as $history)
should be
#foreach($histories as $history)
where $histories is filled in in your FrontController.
$histories = History::all();
When actually getting your single history object, I agree with Sapnesh's answer that you best doublecheck whether or not the object actually exists.
The error occurs because find() returns NULL when a model is not found.
find($id) takes an id and returns a single model. If no matching model
exist, it returns null.
findOrFail($id) takes an id and returns a single model. If no matching
model exists, it throws an error.
In your show() method,
Change:
$history = History::find($id);
To:
$history = History::findOrFail($id);
I'm new to Laravel and am building a simple web application. I'll show what code I'm using next and then I'll explain my problem.
Here's how my form starts in the view login.blade.php:
<?php
//build the form
echo Form::open(array('action' => 'AuthenticationController#authenticateUser'));
And here's what the route for the home page:
Route::get('/', function() {
return View::make('login', array('page_title' => 'Log in to MANGER'));
});
Finally, here's the authentication controller (for now it's a simple redirect to simulate login):
class AuthenticationController extends BaseController {
public function authenticateUser()
{
//retrive from database
return View::make('hr', array('page_title' => 'MANGER Login'));
}
}
My problem is, I'm getting an error on login.blade.php. saying: Route [AuthenticationController#authenticateUser] not defined. (View: /opt/lampp/htdocs/manger/app/views/login.blade.php)
How is the error about a route when I've defined a controller instead? And also, how can this be fixed? Please excuse any noob errors and thanks a lot in advance! :-)
You still need to define your route like this:
Route::post('authenticate', ['uses' => 'AuthenticationController#authenticateUser']);
Otherwise it won't know what method to use, or the url to create.