lundi 19 mars 2018

Laravel 5.3 soft deletes not working when defining a constructor for the model

I have a model Test as follows

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Test extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
public function __construct() {
    if (!\App::environment('production')) {
        $this->table='test_stage';
    }
}

I have made sure that there is a 'deleted_at' column in my test_stage table. But the soft deletes are not working. Using the delete() method permanently removes the record from the table. As an additional step of verification I manually added 'deleted_at' value for some columns. But query the model still gives me the soft deleted record.

Moreover, removing the model constructor entirely and simply defining the table name using:

protected $table = 'test_stage';

Works like a charm! That is soft deletes magically start working again.

Or is there any way around to define the table name according to the environment without the need of defining a constructor?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2pnf1PN
via IFTTT

Aucun commentaire:

Enregistrer un commentaire