小编Men*_*ena的帖子

自定义 Spatie Laravel-Permission 异常消息

我在 Laravel 5.8 API 应用程序中添加了Saptie Laravel 权限包。一切正常,当非管理员用户尝试访问管理员特定路由时,我会遇到异常。

但是,默认异常呈现为 HTML 403 User does not have the right roles。考虑到我在 API 应用程序中使用它,我想为此类异常返回我自己的自定义消息。

我尝试检查是否auth()->user()->hasRole('admin')仍然获得相同的默认异常页面。这是我的代码

路线

Route::post('products', 'ProductController@store')->middleware('role:super-admin|admin'); // create a new product
Run Code Online (Sandbox Code Playgroud)

控制器方法

if (auth()->user()->hasRole('admin')) {

    // create & store the product
    $product = Product::create($request->all())

    // return new product
    $responseMessage    = 'Successful operation';
    $responseStatus     = 200;
    $productResource    = new ProductResource($product);

    return response()->json([
        'responseMessage'   => $responseMessage,
        'responseStatus'    => $responseStatus,
        'product'           => $productResource
    ]);
} else {

    return response()->json([
        'responseMessage'   => …
Run Code Online (Sandbox Code Playgroud)

php laravel spatie

3
推荐指数
1
解决办法
2352
查看次数

在Laravel 5.4中,auth尝试失败

我有一些关于auth尝试失败的帖子,但我的情况似乎有所不同.仍处于开发阶段,所以我的密码是纯文本.我尝试登录,但我一直变错,所以重定向回登录页面.

错误消息显示用户名/密码不匹配,但dd显示电子邮件和密码都正确.

什么可能导致这种失败?PS:这是我第一次使用laravel

web.php

Route::post('/login', 'AuthController@authenticate');
Route::get('/', 'PostController@index');
Run Code Online (Sandbox Code Playgroud)

AuthController

public function auth()
{
    //dd($request);

    // attempt to login the user
    if (! auth()->attempt(request(['email', 'password']))) {

        return back()->withErrors([
            'message' => 'Username/Password does not macth'
        ]);
    }

    return redirect('/');
}
Run Code Online (Sandbox Code Playgroud)

PostController中

public function index()
{
    $posts = Post::latest()->limit(3)->get();

    return view('post.index', compact('posts'));
}
Run Code Online (Sandbox Code Playgroud)

laravel-5

2
推荐指数
1
解决办法
3556
查看次数

设置回调 url 来接收信息

我正在尝试弄清楚如何设置回调网址以从支付网关接收支付状态信息。

这是整个设置的摘要。有一个使用 Laravel 5.8 构建的后端(API 端点),将由 React 应用程序和移动应用程序使用。这两个应用程序将使用支付网关的网络和移动 SDK 来发起支付交易。

然后支付网关应该通过调用我需要设置的回调 api 来响应支付状态。

这是我第一次尝试做这样的事情,所以如果我离得很远,请原谅我,但这是我写的

回调端点

Route::post('order/payment/status', 'PaymentController@getStatus'); // receives payment status
Run Code Online (Sandbox Code Playgroud)

支付控制器getstatus()方法

public function getStatus(Request $request)
    {
        $paymentMethod          = $request->paymentMethod;
        $amount                 = $request->amount;
        $providerCode           = $request->provider;
        $customerName           = $request->customerName;
        $customerEmail          = $request->customerEmail;
        $paymentDescription     = $request->paymentDescription;
        $paymentStatus          = $request->paaymentStatus;
        $transactionReference   = $request->transactionReference;

        // update my record based on the paymentStatus
    }
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?我觉得这应该是一个get请求而不是一个post。请帮忙。

php laravel laravel-5.8

2
推荐指数
1
解决办法
4883
查看次数

无法在Laravel中为具有关系的数据库表播种

在laravel 5.8应用程序中,我想为用户和产品表添加种子。这样的用户和产品之间存在联系

User.php模型(用户可以拥有一个或多个产品)

public function products()
{
    return $this->hasMany(Product::class, 'user_id');
}
Run Code Online (Sandbox Code Playgroud)

Product.php模型(一种产品可以属于一个或多个用户)

public function users()
{
    return $this->belongsToMany(User::class);
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用UsersTableSeeder以下内容同时播种用户表和产品表

public function run()
{
    factory(App\User::class, 3)->create()->each(function ($user) {
        $user->products()->save(factory(App\Product::class, 3)->make());
    });
}
Run Code Online (Sandbox Code Playgroud)

而“ DatabaseSeeder”看起来像这样

public function run()
{
    $this->call(UsersTableSeeder::class);
}
Run Code Online (Sandbox Code Playgroud)

当我运行命令时php artisan db:seed,仅种子用户表,并且出现此错误

Symfony \ Component \ Debug \ Exception \ FatalThrowableError:传递给Illuminate \ Database \ Eloquent \ Relations \ HasOneOrMany :: save()的参数1必须是Illuminate \ Database \ Eloquent \ Model的实例,Illuminate \ Database \ Eloquent \ Collection的实例给定,在第15行的C:\ Users \ …

php laravel laravel-5.8

1
推荐指数
1
解决办法
79
查看次数

标签 统计

laravel ×3

php ×3

laravel-5.8 ×2

laravel-5 ×1

spatie ×1