首頁  >  問答  >  主體

擴展用戶模型以利用 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 天前349

全部回覆(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
  • 取消回覆