Route Model Binding not working Laravel 6 - laravel

Route Model Binding not working
model: Accommodation
Controller: AccommodationController
Route::resource('accommodation', 'AccommodationController');
Working
public function index()
{
$accommodation = Accommodation::get();
return view('admin.accommodations.index', compact('accommodation'));
}
Not working
public function index(Accommodation $accommodation)
{
return view('admin.accommodations.index', compact('accommodation'));
}

You can see your route list in your php artisan terminal to avoid confusion.Use command.
php artisan route:list
Then see in name list what was name , then put in your route function.
However, I think your route will be
admin.accommodation
remove 's'.

Related

Laravel Policy not working on route middleware

I have a NotificationPolicy with the following code:
<?php
namespace App\Policies;
use App\Notification;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class NotificationPolicy
{
use HandlesAuthorization;
public function update(User $user, Notification $notification)
{
return $user->id === $notification->user_id;
}
}
I have registered this properly by adding this to the AuthServiceProvider:
protected $policies = [
Notification::class => NotificationPolicy::class,
];
I have this so that only the logged in user can update their notification by doing things such as setting the archived_at value or read_at value to the current timestamp. The policy does work if I use it in the controller, i.e.;
class ArchiveItemController extends Controller
{
public function __invoke(Notification $notification)
{
$this->authorize('update', $notification);
$notification->markAsArchived();
return redirect()->route('inbox.index')->with('success', 'Item has been archived');
}
}
However I don't want to use them in the controllers and would prefer to use them in my routes file. So I have removed this line $this->authorize('update', $notification); from the controller and I have tried the following but it doesn't work:
Route::prefix('inbox')->middleware(['auth', 'can:employee'])->group(function () {
Route::get('/notification/{notification}/archive', 'User\Account\Inbox\ArchiveItemController')
->name('inbox.item.archive')
->middleware('can:update', 'notification');
});
I've even ran the following but they don't make a difference:
php artisan optimize
php artisan cache:clear
php artisan route:cache
php artisan view:clear
php artisan config:cache
Your middleware declaration is not correct.
You will have to change 'can:update', 'notification' to 'can:update,notification' for it to work:
So in the end you would have the following:
Route::prefix('inbox')->middleware(['auth', 'can:employee'])->group(function () {
Route::get('/notification/{notification}/archive', 'User\Account\Inbox\ArchiveItemController')
->name('inbox.item.archive')
->middleware('can:update,notification');
});
If you have cached the routes, you will have to run php artisan route:clear for the changes to take effect.
From the docs:
Laravel includes a middleware that can authorize actions before the
incoming request even reaches your routes or controllers. By default,
the Illuminate\Auth\Middleware\Authorize middleware is assigned the
can key in your App\Http\Kernel class. Let's explore an example of
using the can middleware to authorize that a user can update a blog
post:
use App\Post;
Route::put('/post/{post}', function (Post $post) {
// The current user may update the post...
})->middleware('can:update,post');
In this example, we're passing the can middleware two arguments. The
first is the name of the action we wish to authorize and the second is
the route parameter we wish to pass to the policy method. In this
case, since we are using implicit model binding, a Post model will be
passed to the policy method. If the user is not authorized to perform
the given action, a HTTP response with a 403 status code will be
generated by the middleware.

Target class [App\Http\Controllers\LoginController] does not exist

Laravel 6.x.
I'm creating custom multi-authentication Panels(Staff,Student,Admin) from Single Login Page.Error already mentioned in the title. also without using php artisan ui bootstrap --auth.
Web.php file.
Route::get('/index/sign-in', function () {
return view('log-in');
});
Route::get('/index/admin', function () {
return view('admin-dashboard');
});
Route::get('/index/student', function () {
return view('student-dashboard');
});
Route::get('/index/staff', function () {
return view('faculty-dashboard');
});
Route::middleware('auth')->group(function () {
Route::post('/index/dashboard/','LoginController#postlogin')->name('postlogin');
Route::post('/index/logout','LoginController#postlogout')->name('postlogout');
});
LoginController.php file
public function postlogout()
{
auth()->logout();
//session()->flash('message', 'Some goodbye message');
return redirect('/index/sign-in/');
}
public function postlogin()
{
$role=(Auth::user())->user_role;
if ($role=='admin'){
return 'index/admin';
}
elseif ($role=='staff'){
return 'index/staff';
}
elseif ($role=='student'){
return 'index/student';
}else
return 'index/sign-in';
}
}
If you didn't move the controller from his default location, is inside the Auth folder, in the controller folder (app/Http/Controllers/Auth)
So as your error Target class [App\Http\Controllers\LoginController] does not exist it's searching for the controller in the Controllers folders but not in the Auth subfolder, so The route is wrong.
Route::post('/index/logout','LoginController#postlogout')->name('postlogout');
should be
Route::post('/index/logout','Auth\LoginController#postlogout')->name('postlogout');
Best Regards.
I'm not sure if this is the default in Laravel 6, but the LoginController is likely under the Auth folder/namespace`:
app
- Http
-- Controllers
--- Auth
---- LoginController.php
...
In this case, you need to reference the namespace in your routes:
Route::middleware('auth')->group(function () {
Route::post('/index/dashboard/', 'Auth\LoginController#postlogin')->name('postlogin');
Route::post('/index/logout', 'Auth\LoginController#postlogout')->name('postlogout');
});
In your web.php file check all namespace passed, if their directory root is passed correctly or not.
in the upper part of web.php file you need to make sure of each controller root.
Such as you are using LoginController so you must pass use App\Http\Controllers\LoginController; or use App\Http\Controllers\Controller\LoginController;
according to your Http\Controller project structure
If you did not create the controller with the artisan command, delete it and create it with the php artisan create:controller LoginController command. This should get the problem resolved.
Can you check the namespace of the controller
namespace App\Http\Controllers\Auth;
class LoginController extends Controller
All the above solution didn't work for me, what did was using
php artisan route:clear

send get request to specific file

I have a file called: test.php located in public/inc/test.php. Through jQuery, I can make a get request through jQuery ($.get), by typing:URL: "public/inc/test.php?" + param1` etc.
I want to do the same, but through Laravel. I want to have a route called: /sendparams/{param1}/{param2}, and do a get request to the test.php file from inside a route in web.php and pass the params variables.
How should the route be written?
edit:
by implementing the classes in laravel as external classes, this is no more needed.
you have in your laravel project a file named web.php inside the routes folder.
Define within the web.php file the below code:
Route::get('/sendparams/{param1}/{param2}', 'ControllerName#index');
Perform the below command to create the controller
php artisan make:controller ControllerName
Open ControllerName (app/Http/Controllers/ControllerName)
Your new controller has an structure like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MailController extends Controller
{
public function __construct()
{
}
/* This is the method executed in your route defined in web.php*/
public function index(Request $request) {
$param1 = $request->param1;
$param2 = $request->param2;
$allParams = $request->all(); // returns an associative array with all params
}
}
you can check your routes performed the below command
php artisan route:list
Regards

How to show page via resource controller laravel 5?

Home.blade.php is starting when I route it directly but not when I call it from resource controller !!
Route::resource('list','listcontroller');
And I call it in index method :
class listcontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return View('list.Home');
}
Change your index function to the following-
public function index()
{
return View('Home');
}
And you will get the corresponding view at base_url/list
or if you want to get with /list/home url then change your route to-
Route::resource('list/home','listcontroller');
If you want to set a base_url othen you can use route prefix
Route::group(['prefix' => 'list'], function () {
Route::resource('home','listcontroller');
});
Any view should be addressed by their path from view folder separated by . (Dot) and with correct character case.
For example if you have home.blade.php in view folder you should call it by view(“home”).
Beside that you can address to any routes by “route” helper function and passing route name to it.
As #amini.swallow said you can reach to your route list by running php artisan route:list command.
In your case you can create link like this :
<a href=“{{route(“list.index”)}}”>click here</a>
Hope it helps.
1)Your controller name is not standard write listController.
2)make sure about your route names:
php artisan route:list
3)in blade write
and try it

Is it possible with laravel to go from post.store to post.edit?

I can't figure out how to go from a store method directly to a edit page.
Routes:
Route::post('/', 'PostController#store')->name('posts.store');
Route::get('/', 'PostController#edit')->name('posts.edit');
post controller:
public function store(Request $request)
{
$post = new Post;
$post->save();
return view('posts.edit');
}
public function edit($id)
{
dd('edit post');
}
I keep getting view not found or other errors. I have checked php artisan route:list and the correct route is there. What am i missing here?
Well you should do a redirect to the route instead. See Redirecting to Named Routes
So this should work:
return redirect()->route('posts.edit');
At this point, you are trying to load a view.

Resources