Answer the question
In order to leave comments, you need to log in
Edit sql before database query?
Hello!
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 for convenience, everything was moved to the Models folder, it used to be in app/
And the corresponding namespace has changed a bit from App\User to App\Models\User.
And we have that polymorphic relations do not work. now there is a request to the model_type column with App\Models\User data, and everything is saved in the old database App\User.
Question: Is it possible to somehow edit this data before querying the database?
type: str_replace('\Models', '', $className)
And then, when the project is completed and ready for public, update the information in the database so that it is as it should be.
Thank you!
Answer the question
In order to leave comments, you need to log in
Nothing prevents you from leaving the App\User model,
just new ones will be generated in models
or DB::table('table')->update(['model_type' => 'App\Models\User'])
This should have been taken care of in the first place.
In general, there is an easy solution to the problem by adding the AppServiceProvider::register() method:
Relation::morphMap([
'model' => Model::class,
]);
public function account()
{
return $this->belongsTo(Account::class, 'target_id')->where('target_type', Account::$morphClass);
}
public static $morphClass = 'account';
Relation::morphMap([
Model::$morphClass => Model::class,
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question