首页  >  问答  >  正文

扩展用户模型以利用 MongoDB

我正在尝试同时使用 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粉293341969P粉293341969234 天前346

全部回复(1)我来回复

  • P粉976737101

    P粉9767371012024-03-23 00:52:22

    我正在使用 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)

    回复
    0
  • 取消回复