A _Construct on an Eloquent Laravel Model

A __construct on an Eloquent Laravel Model

You need to change your constructor to:

public function __construct(array $attributes = array())
{
parent::__construct($attributes);

$this->directory = $this->setDirectory();
}

The first line (parent::__construct()) will run the Eloquent Model's own construct method before your code runs, which will set up all the attributes for you. Also the change to the constructor's method signature is to continue supporting the usage that Laravel expects: $model = new Post(['id' => 5, 'title' => 'My Post']);

The rule of thumb really is to always remember, when extending a class, to check that you're not overriding an existing method so that it no longer runs (this is especially important with the magic __construct, __get, etc. methods). You can check the source of the original file to see if it includes the method you're defining.

Laravel eloquent issue with constructor

Well yes, that's because you override the Eloquent constructor which is responsible to fill the model with values when an array is passed. You have to pass them along to the parent with parent::__construct():

public function __construct(array $attributes = array()){
parent::__construct($attributes);
$this->active = Config::get('app.ActiveFlag');
}

Laravel Eloquent Models __construct method to call relations

belongsTo() defines a relationship, it doesn't load it.

First you need to define the relationship, then you can load it at any point using the load method.

class AdminLog extends Model {
public function user() {
return $this->belongsTo(\App\User::class, 'admin_id');
}
}

$log = AdminLog::first();
$log->load('user');

It is possible to load inside the constructor, but I would highly recommend against that. If you have 20 AdminLog objects then it will query the database 20 times, once for each object. That's inefficient.

What you should do instead is use eager loading. This will query the users table just once for all 20 admin logs. There are many ways to do this, here is an example:

$logs = AdminLog::take(20)
->with('user')
->get();
dd($logs->toArray());

What does parent::__construct($model) functionality?

parent is a PHP keyword use call a property or method of a parent class.

So parent::__construct($model) will call the __construct method of the parent class (BaseRepository, here).

When extending a class, it is sometimes necessary to call the parent constructor, when, let's say, the parent class is also initialising some properties needed. Instead of initialising them in child class constructor, you just need to call the parent class constructor and pass to it the necessary arguments.

Now in your question, this->model = $model; is not really needed in the child constructor (constructor in CategoryRepository) because it will be already initialized when call to parent::__construct($model) and will be accessible in the child class.

this->model = $model; will actually be necessary if $model in BaseRepository was a private property. Then it will not be available in the child class CategoryRepository even if the parent constructor was called.

Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given,

The problem here is your constructor. In fact you don't call "constructor" that you defined but parent constructor is executed. This is because you miss one underscore, it should be __construct.

However it's not good idea to do it like this. First of all, you should call parent constructor, and in addition such custom constructor can cause problems in some scenarios.

It's much better to add method like this:

public static function restoreCart($oldCart)
{
$cart = new static();

if ($oldCart) {
$cart->items = $oldCart->items;
$cart->totalQty= $oldCart->totalQty;
$cart->totalPrice = $oldCart->totalPrice;
}

return $cart;
}

and then in your controller instead of:

$cart = new Cart($oldCart);

you can use:

$cart = Cart::restoreCart($oldCart);

Constructor in laravel model extending eloquent

You can always call the parent constructor inside your constructor:

class MyModel extends Eloquent {

function __construct() {
parent::__construct();
}
}

edit: this will call the following constructor for you https://github.com/illuminate/database/blob/v4.2.17/Eloquent/Model.php#L243

Sometimes you want to call your own code before the parent constructor call and sometimes after, I hope that this link helps determine which of 2 situations best suits your needs!



Related Topics



Leave a reply



Submit