mongodb laravel get error of Call to a member function prepare()on null

uplii1fm  于 2023-11-17  发布在  Go
关注(0)|答案(5)|浏览(173)

在Laravel中,我在MongoDB数据库中插入记录时遇到了这个错误。
在null上调用成员函数prepare()

namespace App\Http\Controllers;

use App\Employee;
use Illuminate\Http\Request;

class EmployeeController extends Controller
{

  public function create()
  {
    return view('addemployee');
  }

  public function store(Request $request)
  {
    $employee = new Employee();
    $employee->firstName = $request->get('firstName');
    $employee->middleName = $request->get('middleName');
    $employee->lastName = $request->get('lastName');
    $employee->gender = $request->get('gender');
    $employee->city = $request->get('city');
    $employee->localAddress = $request->get('localAddress');
    $employee->permanentAddress = $request->get('permanentAddress');
    $employee->emailAddress = $request->get('emailAddress');
    $employee->mobileNumber = $request->get('mobileNumber');
    $employee->email = $request->get('email');
    $employee->department = $request->get('department');
    $employee->designation = $request->get('designation');
    $employee->save();

    return redirect('addemployee')->with('Success', 'Data Inserted Successfully!');

  }
}

字符串
这是我的员工模型类:

<?php
namespace App; 
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Employee extends Eloquent
{
  protected $connection = 'mongodb';
  protected $collection = 'employee';
  protected $fillable = [
    'firstName',
    'middleName',
    'lastName',
    'gender',
    'city',
    'localAddress',
    'parmanentAddress',
    'emailAddress',
    'mobileNumber',
    'email',
    'department',
    'designation'
  ];
}

hsgswve4

hsgswve41#

确保

  • 该集合存在于数据库中
  • 您的模型扩展了Jenssegers\Mongodb\Eloquent\Model,而不是Model

使用Jenssegers\Mongodb\Eloquent\Model作为Eloquent;
class Node extends Eloquent { }

k75qkfdt

k75qkfdt2#

如果使用jenssegers/laravel-mongodb包,这应该可以解决问题:
config/auth中,确保正确设置model值 *(如果未使用默认User模型位置 *):

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User\User::class,
    ],

字符串
接下来,您的User模型类应该扩展正确的Authenticable(从包中):

use Jenssegers\Mongodb\Auth\User as Authenticatable;
...
class User extends Authenticatable implements JWTSubject { ... }


如果其他一切(警卫等)根据文档配置正确,它应该没有任何问题。

gr8qqesn

gr8qqesn3#

地址:广州市白云区白云大道118号电话:0755 - 8888888传真:0755 - 8888888
在我的例子中,我已经更改了域,但没有将.env中的APP_URL更新为新域。

s5a0g9ez

s5a0g9ez4#

在你的模型中,你写了protected $collection而不是protected $table
顺便说一下,这可能与您的错误没有联系,但您在$fillable属性中写入了 parmanentAddress 而不是 permanentAddress。这可能会引发其他错误,因为您也在Controller中更新此字段。

固定员工模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Employee extends Eloquent
{
    protected $connection = 'mongodb';

    protected $table = 'employee';

    protected $fillable = [
        'firstName',
        'middleName',
        'lastName',
        'gender',
        'city',
        'localAddress',
        'permanentAddress',
        'emailAddress',
        'mobileNumber',
        'email',
        'department',
        'designation'
    ]; 
}

字符串

mpgws1up

mpgws1up5#

如果你正在使用using mongodb/laravel-mongodb,请确保你正在从MongoDB\Laravel\Eloquent\Model扩展你的模型;

相关问题