在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'
];
}
型
5条答案
按热度按时间hsgswve41#
确保
使用Jenssegers\Mongodb\Eloquent\Model作为Eloquent;
class Node extends Eloquent { }
k75qkfdt2#
如果使用jenssegers/laravel-mongodb包,这应该可以解决问题:
在
config/auth
中,确保正确设置model
值 *(如果未使用默认User
模型位置 *):字符串
接下来,您的
User
模型类应该扩展正确的Authenticable
(从包中):型
如果其他一切(警卫等)根据文档配置正确,它应该没有任何问题。
gr8qqesn3#
地址:广州市白云区白云大道118号电话:0755 - 8888888传真:0755 - 8888888
在我的例子中,我已经更改了域,但没有将.env中的APP_URL更新为新域。
s5a0g9ez4#
在你的模型中,你写了
protected $collection
而不是protected $table
。顺便说一下,这可能与您的错误没有联系,但您在
$fillable
属性中写入了 parmanentAddress 而不是 permanentAddress。这可能会引发其他错误,因为您也在Controller中更新此字段。固定员工模型
字符串
mpgws1up5#
如果你正在使用using
mongodb/laravel-mongodb
,请确保你正在从MongoDB\Laravel\Eloquent\Model扩展你的模型;