Reputation: 1674
I'm trying to call add an item to a model through my public folder in Laravel but I'm getting this error:
Fatal error: Uncaught Error: Call to a member function connection() on null in [..]/Illuminate/Database/Eloquent/Model.php
Here is the file in my public folder:
<?php
require __DIR__.'../../../bootstrap/autoload.php';
use Illuminate\Database\Eloquent\Model;
use \App\Models\Cdr;
$id_ex = '11';
$cdr = new Cdr();
$cdr->id_ex = $id_ex;
$cdr->save();
Do I need to start the app somehow before this? I've also tried calling a method inside a controller from the public folder, but it gives me the same error. For example:
CdrController::storeCdr($id_ex);
My model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Cdr extends Model
{
public $table = 'cdr';
protected $dates = ['deleted_at'];
public $fillable = [];
public $guarded = ['id'];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [];
/**
* Validation rules
*
* @var array
*/
public static $rules = [];
}
Upvotes: 1
Views: 2039
Reputation: 4135
Right now, your script hasn't got a clue of it's environment. It doesn't know there is a database or even a framework for that matter, you have to actually boot the app to make this information available.
You can do this with
$app = require __DIR__ . '../../../bootstrap/app.php';
If this doesn't work, you need to boot at least the ConsoleKernel. Check app/Console/Kernel.php
and it's superclass ConsoleKernel
for an example how to do that.
Upvotes: 1