扩展用户模型以利用 MongoDB
P粉293341969
P粉293341969 2024-03-22 09:21:35
0
1
285

我正在尝试同时使用 Laravel 和 MongoDB。我让他们在未经身份验证的情况下进行交谈,但现在我想添加用户,所以我使用了一些来源和故障排除来汇集:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
//use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    protected $dates = ['email_verified_at'];
}

但我收到此错误:

> {
>     "message": "Call to a member function prepare() on null",
>     "exception": "Error",
>     "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
>     "line": 539,
>     "trace": [
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
>             "line": 753,
>             "function": "Illuminate\Database\{closure}",
>             "class": "Illuminate\Database\Connection",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
>             "line": 720,
>             "function": "runQueryCallback",
>             "class": "Illuminate\Database\Connection",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
>             "line": 534,
>             "function": "run",
>             "class": "Illuminate\Database\Connection",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
>             "line": 498,
>             "function": "statement",
>             "class": "Illuminate\Database\Connection",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php",
>             "line": 32,
>             "function": "insert",
>             "class": "Illuminate\Database\Connection",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
>             "line": 3339,
>             "function": "processInsertGetId",
>             "class": "Illuminate\Database\Query\Processors\Processor",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php",
>             "line": 1869,
>             "function": "insertGetId",
>             "class": "Illuminate\Database\Query\Builder",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
>             "line": 1330,
>             "function": "__call",
>             "class": "Illuminate\Database\Eloquent\Builder",
>             "type": "->"
>         },
>         {
>
>  ..............Trimmed as I assume you dont need it all
>
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
>             "line": 175,
>             "function": "then",
>             "class": "Illuminate\Pipeline\Pipeline",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
>             "line": 144,
>             "function": "sendRequestThroughRouter",
>             "class": "Illuminate\Foundation\Http\Kernel",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/public/index.php",
>             "line": 51,
>             "function": "handle",
>             "class": "Illuminate\Foundation\Http\Kernel",
>             "type": "->"
>         },
>         {
>             "file": "/mnt/api/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php",
>             "line": 16,
>             "function": "require_once"
>         }
>     ] }

请问有什么想法吗?奇怪的是,用户是在我提交帖子时创建的。

P粉293341969
P粉293341969

全部回复(1)
P粉976737101

我正在使用 jenseggers 和 9.2.1

我遇到了同样的问题,我的解决方案是修改用户模型 使用 jenssegers 不打底粉底

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Auth\User as Authenticatable;
//use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;```

That said I just use breeze for the user authentication (DRY)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!