You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found that many of my models do the same things and I decided to do Models optimisation. So I moved all similar functions to trait.
CategoryTrait.php
public function __construct() { $this->model = app($this->getModelClass()); }
abstract protected function getModelClass();
public function items() { return $this->hasMany($this->model, 'category_id'); }
NewsCategory.php
use App\Models\News as ItemModel;
use Illuminate\Database\Eloquent\Model;
use App\Models\ModelTraits\CategoryTrait;
class NewsCategory extends Model
{
use CategoryTrait;
protected $model;
protected function getModelClass() { return ItemModel::class; }
}
In general all is OK. Web-site works in the same way. But import from excel is not working anymore.
When I have code of the Model like above excel import gives me following error SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'news_categories_slug_unique' (SQL: insert into news_categories () values ())
And in news_categories table I can see one row filled with NULL values
If I remove unique restriction from slug field then news_categories table will be seeded by the number of rows, which I have in excel, but instead of values from excel everywhere will be NULL
So at the moment I decided to return back to normal Model structure.
Just interesting if someone could explain to me why this happened.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I found that many of my models do the same things and I decided to do Models optimisation. So I moved all similar functions to trait.
CategoryTrait.php
NewsCategory.php
In general all is OK. Web-site works in the same way. But import from excel is not working anymore.
When I have code of the Model like above excel import gives me following error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'news_categories_slug_unique' (SQL: insert into
news_categories
() values ())And in news_categories table I can see one row filled with NULL values
If I remove unique restriction from slug field then news_categories table will be seeded by the number of rows, which I have in excel, but instead of values from excel everywhere will be NULL
So at the moment I decided to return back to normal Model structure.
Just interesting if someone could explain to me why this happened.
Beta Was this translation helpful? Give feedback.
All reactions