I need to use policy for crud controller in laravel backpack crud package.
I use :
$this->authorizeResource(Post::class);
and i get this error :
Method
App\Http\Controllers\Admin\PostCrudController::authorizeResource does
not exist.
How should i use Policy (specially resource policy) in laravel backpack crud?
You should be able to do that using Laravel's AuthorizesRequests trait. Generated Backpack CRUD Controllers don't have it by default, since not everybody uses this Laravel feature.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\MonsterRequest as StoreRequest;
use App\Http\Requests\MonsterRequest as UpdateRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class MonsterCrudController extends CrudController
{
use AuthorizesRequests;
public function setup()
{
// ...
Related
I'm developing an eccomerce site. and im getting this error on checkout. please help
here are the defined routes:
//payfast payment
Route::get('payment', 'PaymentController#confirmpayment')->name('confirmPayment');
Route::get('/payfast/success','PaymentController#success')->name('payment.success');
Route::get('/payfast/cancel','PaymentController#cancel')->name('payment.cancel');
and here is the Controller (destination route):
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use NunoMaduro\Collision\Provider;
use App\Models\Cart;
use App\Models\Product;
use DB;
use Billow\Contracts\PaymentProcessor;
Class PaymentController extends Controller
{
public function confirmpayment(PaymentProcessor $payfast)
{
$cart = Cart::where('user_id',auth()->user()->id)->where('order_id',null)->get()->toArray();
$data = [];
When you use Laravel Named Route name(), you have to use it.
Route::get('payment', 'PaymentController#confirmpayment')->name('confirmPayment');
Instead of using
payment
replace it with
confirmPayment
change your HTTP method from get to post
like this:
Route::post('payment', 'PaymentController#confirmpayment')->name('confirmPayment')
it seems you don't have route named payment you only have
confirmPayment
payment.success
payment.cancel
I make:
php artisan make:request DiscoverRequest
I want use DiscoverRequest instead default NovaRequest for create new entity for specific resource.
In Laravel Nova exist unified ResourceStoreController for all resources.
public function handle(CreateResourceRequest $request)
Route::post('/{resource}', 'ResourceStoreController#handle');
I want to override Request only for one resource.
How this can be implemented?
Can you elaborate what you mean by "override Request for only one resource"?
Normally you can just type hint the new Request in the method like this:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreItineraryRequest;
class ResourceStoreController extends Controller
{
public function handle(DiscoverRequest $request) {
...
}
}
I have a problem with Laravel notifications. I try to give a user notification about something, but Laravel cannot find notification class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class NotificationController extends Controller
{
public function getNot(Request $request)
{
$user = Auth::user();
$user->notify(new NewPost('a'));
}
}
I've also created a notification with the name NewPost.php, the problem is:
Class 'App\Http\Controllers\NewPost' not found
this one, so in the User model already included Notifications and notifiable.
Add use statement before class definition.
use Illuminate\Http\Request;
use Auth;
use App\Notifications\NewPost;
I assume that you create notification by artisan, if no, then keep in mind that namespace could be different.
Suppose some controller like this:
<?php
namespace App\Http\Controller
use Illuminate\Http\Request;
use Validator;
use Auth;
class MemberController extends Controller {
//some code
}
Where do Validator and Auth belong to (so I can see their defintion) and what exactly are they?
I've one more question: here (Laravel 5.6 documentaton) it says
we will use the validate method provided by the
Illuminate\Http\Request object.
and when I check here for more information, there's no validate method!
I'd really appreciate it if anyone can help me with these questions.
Validator and Auth are aliases for the Facades of the same name in Illuminate\Support\Facades. They are a static proxy for an instance of a class. You can read about them in the Laravel docs about Facades.
The aliases for these are configured in config/app.php 'aliases' array.
Laravel 5.6 Docs - Facades
For $request->validate(...):
It is a macro. Macros allow you to add functionality to classes that implement macro functionality at run time.
The Illuminate\Foundation\Providers\FoundationServiceProvider#registerRequestValidation sets this macro on Illuminate\Http\Request to allow for a validate method.
/**
* Register the "validate" macro on the request.
*
* #return void
*/
public function registerRequestValidation()
{
Request::macro('validate', function (array $rules, ...$params) {
return validator()->validate($this->all(), $rules, ...$params);
});
}
For a quick read about macros in Laravel check out my article:
asklagbox blog - Using Macros in Laravel
Is there an easy way to override the Laravel 5.3 Passport Models so they would use Vinelab\NeoEloquent\Eloquent\Model instead of Illuminate\Database\Eloquent\Model
You can give a try and bind it in your service provider in register method:
public function register()
{
$this->app->bind(\Illuminate\Database\Eloquent\Model::class, \Vinelab\NeoEloquent\Eloquent\Model::class)
}