Edit sql before querying the database?
There is a working project on version 5.8
Now we are doing a global update of the project, including laravel to the latest version.
In the latest version, it turns out that everything was divided for convenience and moved to the Models folder, it used to be in app /
And the corresponding namespace has changed slightly from App \ User to App \ Models \ User.
And so we have that polymorphic relationships do not work. now there is a request to the column model_type with the data App \ Models \ User, and in the old database everything is saved App \ User.
Question: Is it possible to somehow edit this data before requesting the database?
type: str_replace ('\ Models', '', $ className)
And then, when the project is completed and ready for the public, update the information in the database to make it right.
Thank you!Laravel Anonymous, Aug 6, 2019
Nothing prevents you from leaving the App \ User model
just new ones will be generated in models
or DB :: table ('table') - & gt; update (['model_type' = & gt; 'App \ Models \ User'])Isaac Fisher
This had to be taken care of initially.
In general, there is an easy solution to the problem by adding a method to the AppServiceProvider :: register ():
'model' => Model::class,
Where model is the name that was before (App \ Models \ User) and the model is the model itself, but I faced some problems when I had to get this name:
public function account()
return $this->belongsTo(Account::class, 'target_id')->where('target_type', Account::$morphClass);
I started adding the name directly to the model:
public static $ morphClass = 'account';
and it turns out like this:
Model::$morphClass => Model::class,
Link to documentation
In general, for now, you can indicate the names that were, but as the update progresses, it is better to replace these names in the database, because long names simply take up more space.Miles West
To place the code, please use CodePen or similar tool. Thanks you!