Home  >  Q&A  >  body text

Extend the user model to take advantage of MongoDB

I'm trying to use Laravel and MongoDB at the same time. I had them talking without authentication but now I want to add the user so I used some sources and troubleshooting to pull this together:

<?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'];
}

But I get this error:

> {
>     "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"
>         }
>     ] }

Do you have any ideas? The strange thing is that the user is created when I submit the post.

P粉293341969P粉293341969234 days ago348

reply all(1)I'll reply

  • P粉976737101

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

    I'm using jenseggers and 9.2.1

    I encountered the same problem, my solution was to modify the user model Use jenssegers non-priming foundation

    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)

    reply
    0
  • Cancelreply