laravel route::resource can't find edit function in my controller - php

When I call my edit function I get a 404|not found page.
this is the code in my View
<a href="{{ route('admin.edit', $user->id) }}">
<button type="button" class="btn btn-primary btn-sm crudbtn">Edit</button>
</a>
This is the code in my web.php
route::namespace('Admin')->prefix('admin')->middleware(['auth', 'auth.admin'])->name('admin.')->group(function(){
route::resource('/', 'AdminController');
});
This is my edit function in my AdminController
public function edit($id)
{
if(Auth::user()->id == $id){
return redirect()->route('admin.index');
}
return view('admin.edit')->with(['user' => user::find($id), 'roles' => Role::all()]);
}
And this is the output of my php artisan route:list
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+---------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+---------------------+
| | GET|HEAD | / | | Closure | web |
| | POST | admin | admin.store | App\Http\Controllers\Admin\AdminController#store | web,auth,auth.admin |
| | GET|HEAD | admin | admin.index | App\Http\Controllers\Admin\AdminController#index | web,auth,auth.admin |
| | GET|HEAD | admin/create | admin.create | App\Http\Controllers\Admin\AdminController#create | web,auth,auth.admin |
| | DELETE | admin/{} | admin.destroy | App\Http\Controllers\Admin\AdminController#destroy | web,auth,auth.admin |
| | PUT|PATCH | admin/{} | admin.update | App\Http\Controllers\Admin\AdminController#update | web,auth,auth.admin |
| | GET|HEAD | admin/{} | admin.show | App\Http\Controllers\Admin\AdminController#show | web,auth,auth.admin |
| | GET|HEAD | admin/{}/edit | admin.edit | App\Http\Controllers\Admin\AdminController#edit | web,auth,auth.admin |
When i call my index function the proper page is loaded, but I can't figure out why my edit function cant' be found. What am I missing or doing wrong?

Try
route::namespace('Admin')->middleware(['auth', 'auth.admin'])->group(function(){
route::resource('admin', 'AdminController');
});

Related

Laravel 8 getting ReflectionException error

in Laravel 7 which i installed and i used nWdart module, i didn't have any problem. now after migrating project to Laravel 8 i have some issue which one of them is i get ReflectionException error
when i try to running php artisan route:list i get below error:
1 [internal]:0
Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))
2 C:\xampp\htdocs\v8\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
ReflectionException::
("Class Modules\Media\Http\Controllers\Modules\Media\Http\Controllers\Modules\Media\Http\Controllers\ImagesController does not exist")
php artisan module:list command output:
+-------------+---------+-------+----------------------------------------+
| Name | Status | Order | Path |
+-------------+---------+-------+----------------------------------------+
| Blog | Enabled | | C:\xampp\htdocs\v8\Modules/Blog |
| Media | Enabled | | C:\xampp\htdocs\v8\Modules/Media |
| Profile | Enabled | | C:\xampp\htdocs\v8\Modules/Profile |
| Radio | Enabled | | C:\xampp\htdocs\v8\Modules/Radio |
| Store | Enabled | | C:\xampp\htdocs\v8\Modules/Store |
| Ticket | Enabled | | C:\xampp\htdocs\v8\Modules/Ticket |
| UserMessage | Enabled | | C:\xampp\htdocs\v8\Modules/UserMessage |
+-------------+---------+-------+----------------------------------------+
and my routes:
<?php
use Illuminate\Support\Facades\Route;
use Modules\Media\Http\Controllers\ImagesController;
Route::prefix('panel')->group(function () {
Route::prefix('media')->group(function () {
Route::prefix('mediaResource')->group(function () {
Route::resource('imagesController', ImagesController::class);
...
});
});
});
ImagesController class:
<?php
namespace Modules\Media\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
class ImagesController extends Controller
{
public function index()
{
}
//...
public function destroy($id)
{
}
}
| panel/media/mediaResource/imagesController | imagesController.store |
Modules\Media\Http\Controllers\ImagesController#store | web |
| | GET|HEAD | panel/media/mediaResource/imagesController | imagesController.index |
Modules\Media\Http\Controllers\ImagesController#index | web |
| | GET|HEAD | panel/media/mediaResource/imagesController/create | imagesController.create |
Modules\Media\Http\Controllers\ImagesController#create | web |
| | DELETE | panel/media/mediaResource/imagesController/{imagesController}| imagesController.destroy |
Modules\Media\Http\Controllers\ImagesController#destroy| web |
| | PUT|PATCH | panel/media/mediaResource/imagesController/{imagesController}| imagesController.update |
Modules\Media\Http\Controllers\ImagesController#update | web |
| | GET|HEAD | panel/media/mediaResource/imagesController/{imagesController}| imagesController.show |
Modules\Media\Http\Controllers\ImagesController#show | web |
| | GET|HEAD | panel/media/mediaResource/imagesController/{imagesController}/edit | imagesController.edit

Page not Found 404 in laravel

I am trying to route the following dropdown to a specific part of a controller, I keep getting Page not found when going to the link:http://127.0.0.1:8000/assignees/custom?datacenter=RDC03,
here's my route(s):
Route::resource('cards','CardController');
Route::resource('customers','CustomerController');
Route::resource('assignees','AssigneeController');
Route::get('autocomplete', 'AssigneeController#autocomplete')->name('autocomplete');
Route::get('searchcard', 'AssigneeController#searchcard')->name('searchcard');
Route::get('returncard/{id}', 'AssigneeController#returncard')->name('returncard');
Route::get('custom', 'AssigneeController#custom')->name('custom');
here is the view from index.blade.php:
<form action="{{ route('custom') }}">
<div class="form-group" id="dropdown">
<label>Data Center:</label>
<select name="datacenter" id="datacenter" class="form-group">
<option value="RDC46">RDC46</option>
<option value="RDC03">RDC03</option>
<option value="RDC05">RDC05</option>
<option value="DDC21">DDC21</option>
</select>
<button type="submit" class="btn btn-info">Submit</button>
</div>
</form>
and here is my controller:
public function custom(Request $request)
{
// You will have access to the datacenter using $request->datacenter
$assignees = assignee::findorfail($request->datacenter)->paginate(10);
return view('assignees.custom',compact('assignees'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
and here is the path to custom.blade.php:
resources/views/assignees/custom.blade.php
here is my current route:list:
Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+---------------------------+-------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | assignees | assignees.index | App\Http\Controllers\AssigneeController#index | web |
| | POST | assignees | assignees.store | App\Http\Controllers\AssigneeController#store | web |
| | GET|HEAD | assignees/create | assignees.create | App\Http\Controllers\AssigneeController#create | web |
| | DELETE | assignees/{assignee} | assignees.destroy | App\Http\Controllers\AssigneeController#destroy | web |
| | PUT|PATCH | assignees/{assignee} | assignees.update | App\Http\Controllers\AssigneeController#update | web |
| | GET|HEAD | assignees/{assignee} | assignees.show | App\Http\Controllers\AssigneeController#show | web |
| | GET|HEAD | assignees/{assignee}/edit | assignees.edit | App\Http\Controllers\AssigneeController#edit | web |
| | GET|HEAD | autocomplete | autocomplete | App\Http\Controllers\AssigneeController#autocomplete | web |
| | POST | cards | cards.store | App\Http\Controllers\CardController#store | web |
| | GET|HEAD | cards | cards.index | App\Http\Controllers\CardController#index | web |
| | GET|HEAD | cards/create | cards.create | App\Http\Controllers\CardController#create | web |
| | GET|HEAD | cards/{card} | cards.show | App\Http\Controllers\CardController#show | web |
| | PUT|PATCH | cards/{card} | cards.update | App\Http\Controllers\CardController#update | web |
| | DELETE | cards/{card} | cards.destroy | App\Http\Controllers\CardController#destroy | web |
| | GET|HEAD | cards/{card}/edit | cards.edit | App\Http\Controllers\CardController#edit | web |
| | GET|HEAD | custom | assignee.custom | App\Http\Controllers\AssigneeController#custom | web |
| | POST | customers | customers.store | App\Http\Controllers\CustomerController#store | web |
| | GET|HEAD | customers | customers.index | App\Http\Controllers\CustomerController#index | web |
| | GET|HEAD | customers/create | customers.create | App\Http\Controllers\CustomerController#create | web |
| | PUT|PATCH | customers/{customer} | customers.update | App\Http\Controllers\CustomerController#update | web |
| | DELETE | customers/{customer} | customers.destroy | App\Http\Controllers\CustomerController#destroy | web |
| | GET|HEAD | customers/{customer} | customers.show | App\Http\Controllers\CustomerController#show | web |
| | GET|HEAD | customers/{customer}/edit | customers.edit | App\Http\Controllers\CustomerController#edit | web |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController#index | web,auth |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController#showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\LoginController#login | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController#logout | web |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController#sendResetLinkEmail | web,guest |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController#showLinkRequestForm | web,guest |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController#reset | web,guest |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController#showResetForm | web,guest |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController#showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController#register | web,guest |
| | GET|HEAD | returncard/{id} | returncard | App\Http\Controllers\AssigneeController#returncard | web |
| | GET|HEAD | searchcard | searchcard | App\Http\Controllers\AssigneeController#searchcard | web
Can you try something like this :
Route::get('/custom', 'AssigneeController#custom')->name('assignee.custom');
Route::resource('cards','CardController');
Route::resource('customers','CustomerController');
Route::resource('assignees','AssigneeController');
Route::get('autocomplete', 'AssigneeController#autocomplete')->name('autocomplete');
Route::get('searchcard', 'AssigneeController#searchcard')->name('searchcard');
Route::get('returncard/{id}', 'AssigneeController#returncard')->name('returncard');
and
<form action="{{ route('assignee.custom') }}">
if this it's because of the Route::resource('assignees','AssigneeController'); that generates a route assignees/{assignee} without any regular expression, so your assignees/custom route is catch with this route
You should try this:
public function custom(Request $request)
{
// You will have access to the datacenter using $request->datacenter
$assignees = assignee::findorfail($request->datacenter)->paginate(10);
return view('assignees.custom',compact('assignees'));
}
Just remove with() and try
Updated Answer
<form action="{{ route('assignee.custom') }}">
<div class="form-group" id="dropdown">
<label>Data Center:</label>
<select name="datacenter" id="datacenter" class="form-group">
<option value="RDC46">RDC46</option>
<option value="RDC03">RDC03</option>
<option value="RDC05">RDC05</option>
<option value="DDC21">DDC21</option>
</select>
<button type="submit" class="btn btn-info">Submit</button>
</div>
</form>

Laravel "Method [index] does not exist."

+--------+----------+-------------------------+-------+--------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------------+-------+--------------------------------------------------------+--------------+
| | GET|HEAD | / | | App\Http\Controllers\HomeController#index | web,auth |
| | POST | / | | App\Http\Controllers\HomeController#newsSubmit | web |
| | POST | announcement/get/fields | | App\Http\Controllers\HomeController#newsGetFields | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | login | login | App\Http\Controllers\UserController#login | web |
| | POST | login/user | | App\Http\Controllers\UserController#loginUser | web |
| | POST | notifications/get | | App\Http\Controllers\notificationsController#getNotif | web |
| | POST | notifications/read | | App\Http\Controllers\notificationsController#readNotif | web |
| | GET|HEAD | notifications/view | | App\Http\Controllers\notificationsController#index | web |
| | GET|HEAD | register | | App\Http\Controllers\UserController#register | web |
| | POST | register/user | | App\Http\Controllers\UserController#registerUser | web |
+--------+----------+-------------------------+-------+--------------------------------------------------------+--------------+
Those are all my routes:
//notifications
Route::post('/notifications/get', 'notificationsController#getNotif');
Route::post('/notifications/read', 'notificationsController#readNotif');
Route::get('/notifications/view', 'notificationsController#index');
And this is my controller:
<?php
namespace App\Http\Controllers;
use App\notifications;
use Illuminate\Http\Request;
class notificationsController extends Controller
{
public function getNotif(Request $request)
{
$id = $request->input('id');
$notifResult = notifications::orderBy('id', 'desc')->where('userID', '=', $id)->where('readed', '=', 0)->get();
$data = array(
'notifResult' => $notifResult,
'notifNumber' => count($notifResult)
);
return $data;
}
public function readNotif(Request $request)
{
$notifid = $request->input('id');
$findNotif = notifications::findOrFail($notifid);
$findNotif->readed = 1;
$findNotif->save();
}
public function index()
{
return view('notifications');
}
}
Only the index one is not working.
I am trying to display all the notifications when clicking on "View all", View all would take the user to /notifications/view which works but when I get there, I get the "Method [index] does not exist." error, and that is, I assume public function index().
SOLVED
I forgot to save the file....
Tried to clear your view and simple cache. It could be due to cache.

Nested Resource : Missing required parameters (laravel)

I am quite new to Laravel, and to be honest - It is a great framework. I am however, a bit confused about some nested resources - including CRUD'ing them.
For routes I am using
Route::resource('startups','StartupsController');
Route::resource('startups.usecases','UsecasesController');
php artisan route:list
| | GET|HEAD | admin/startups | admin.startups.index | App\Http\Controllers\StartupsController#index | web,admin,auth:admin |
| | POST | admin/startups | admin.startups.store | App\Http\Controllers\StartupsController#store | web,admin,auth:admin |
| | GET|HEAD | admin/startups/create | admin.startups.create | App\Http\Controllers\StartupsController#create | web,admin,auth:admin |
| | DELETE | admin/startups/{startup} | admin.startups.destroy | App\Http\Controllers\StartupsController#destroy | web,admin,auth:admin |
| | GET|HEAD | admin/startups/{startup} | admin.startups.show | App\Http\Controllers\StartupsController#show | web,admin,auth:admin |
| | PUT|PATCH | admin/startups/{startup} | admin.startups.update | App\Http\Controllers\StartupsController#update | web,admin,auth:admin |
| | GET|HEAD | admin/startups/{startup}/edit | admin.startups.edit | App\Http\Controllers\StartupsController#edit | web,admin,auth:admin |
| | GET|HEAD | admin/startups/{startup}/usecases | admin.startups.usecases.index | App\Http\Controllers\UsecasesController#index | web,admin,auth:admin |
| | POST | admin/startups/{startup}/usecases | admin.startups.usecases.store | App\Http\Controllers\UsecasesController#store | web,admin,auth:admin |
| | GET|HEAD | admin/startups/{startup}/usecases/create | admin.startups.usecases.create | App\Http\Controllers\UsecasesController#create | web,admin,auth:admin |
| | DELETE | admin/startups/{startup}/usecases/{usecase} | admin.startups.usecases.destroy | App\Http\Controllers\UsecasesController#destroy | web,admin,auth:admin |
| | GET|HEAD | admin/startups/{startup}/usecases/{usecase} | admin.startups.usecases.show | App\Http\Controllers\UsecasesController#show | web,admin,auth:admin |
| | PUT|PATCH | admin/startups/{startup}/usecases/{usecase} | admin.startups.usecases.update | App\Http\Controllers\UsecasesController#update | web,admin,auth:admin |
| | GET|HEAD | admin/startups/{startup}/usecases/{usecase}/edit | admin.startups.usecases.edit | App\Http\Controllers\UsecasesController#edit | web,admin,auth:admin |
For the parent, here called "startups", everything is fine, i can CRUD everything just fine.
For the child, here called "usecases", index, create, and store are fine. The moment I try to add a link to the edit, delete, or show within my templates I start getting : Missing required parameters for [Route: admin.startups.usecases.destroy] [URI: admin/startups/{startup}/usecases/{usecase}].
A link in my index blade file :
<li>Show</li>
returns that error.
In my UsecasesController
public function show($startup_id, $usecase_id)
{
$startups = $startup_id;
$usecase = Usecase::find($usecase_id);
return view('admin.startups.usecases.show')
->with('usecase', $usecase)
->with('startups', $startups);
}
TLDR: I can access and CRUD everything on parent, "startups". I can create and store for the child, but not edit, show, or delete - for "usecases".
Your help is appreciated.
You need to pass parameters as an array:
route('admin.startups.usecases.show', [$startup->id, $usecase->id])
And show() method will look like this:
public function show($startup, $usecase)
{
....

Resource controller returns not found exception on show

In Laravel I have a route as follows:
Route::resource('/admin/products/', 'ProductsController');
So if I go to myapp.app:8000/admin/products/ I get all the products:
public function index()
{
$products = Product::all();
return View::make('products.index', compact('products'));
}
But if I go to myapp.app:8000/admin/products/1 where I have the following:
public function show($id)
{
return "Hi!";
}
I get an error page: NotFoundHttpException. Have I done something wrong?
In case it helps, I have attached my php artisan routes output for the relevant queries:
+--------+---------------------------------+-------------------------+-----------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+---------------------------------+-------------------------+-----------------------------------------+----------------+---------------+
| | GET|HEAD admin/users/{month?} | | AdminController#users | admin | |
| | GET|HEAD admin/products | admin.products..index | ProductsController#index | admin | |
| | GET|HEAD admin/products/create | admin.products..create | ProductsController#create | admin | |
| | POST admin/products | admin.products..store | ProductsController#store | admin | |
| | GET|HEAD admin/products/{} | admin.products..show | ProductsController#show | admin | |
| | GET|HEAD admin/products/{}/edit | admin.products..edit | ProductsController#edit
| | PUT admin/products/{} | admin.products..update | ProductsController#update | admin | |
| | PATCH admin/products/{} | | ProductsController#update | admin | |
| | DELETE admin/products/{} | admin.products..destroy | ProductsController#destroy | admin | |
Looks like it has to do with how Laravel parses the route. Change your route to:
Route::resource('admin/products', 'ProductsController');
And the list of outputs from Artisan should change to something like this:
GET|HEAD admin/products/{products}

Categories