'laravel get error of Call to a member function prepare() on null
In Laravel I have got this error, while inserting a record in my MongoDB database.
Call to a member function prepare() on null
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!');
}
}
This is my Employee Model Class:
<?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'
];
}
Solution 1:[1]
Make Sure
- the collection exists in the database
Your model extends Jenssegers\Mongodb\Eloquent\Model and not Model
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Node extends Eloquent { }
Solution 2:[2]
This should resolve the issue if using jenssegers/laravel-mongodb package:
In config/auth
make sure to have model
value correctly set if not using default User
model location:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User\User::class,
],
Next, your User
model class should extend correct Authenticable
(from the package):
use Jenssegers\Mongodb\Auth\User as Authenticatable;
...
class User extends Authenticatable implements JWTSubject { ... }
If everything else (guard, etc.) was configured right according to docs, it should work without any issue.
Solution 3:[3]
In your model, you wrote protected $collection
instead of protected $table
.
By the way, this as probably no link with your error, but you wrote parmanentAddress instead of permanentAddress in your $fillable
property. This may throw an other error since you are updating this field as well in your Controller.
Fixed Employee Model
<?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'
];
}
Solution 4:[4]
Call to a member function prepare() on null in file /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 359
In my case, I had changed the domain but hadn't updated APP_URL in .env to the new domain.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Imran Omar Bukhsh |
Solution 2 | lbartolic |
Solution 3 | pellul |
Solution 4 | Scotch Design |