php - Why extended class does not have the relationship in Laravel with Modules?
Get the solution ↓↓↓I have a class (
App\Models\
) and an{-code-3}
class (App\Models\{-code-3}
) which have a polymorphic relationship with{-code-5}
(App\Models\{-code-5}
). I am using Modules ({-code-7}
).
In my{-code-8}
in the boot method I have this relationship:
Relation::morphMap(
[
'organizations' => 'App\Models\{-code-3}',
'persons' => 'App\Models\',
]
);
Now, I have a module called{-code-10}
, that module has a model (
Modules\{-code-10}\Entities\
) which extends fromApp\Models\
. I also have a{-code-5}
model (Modules\{-code-10}\Entities\{-code-5}
) which extends fromApp\Models\{-code-5}
.
I added the following code to the{-code-10}ServiceProvider
boot method:
Relation::morphMap(
[
'persons' => 'Modules\{-code-10}\Entities\',
]
);
Now, if I go to any controller and I load a like this:
$person = \App\Models\::find( 1 );
The relationship with{-code-5}
doesn't exist, so I can't do{-code-22}
But if I load a like this:
$person = \Modules\{-code-10}\Entities\::find( 1 );
The relationship works like a charm.
The fun part is that all the relationship methods with users are inside the files {-code-5}.php and .php in the{-code-24}
directory.
In {-code-24}\{-code-5}.php I have:
class {-code-5} extends Authenticatable implements MustVerifyEmail {
public function usable()
{
return $this->morphTo();
}
}
In my {-code-24}\.php
class extends Model {
public function users()
{
return $this->morphMany( {-code-5}::class, 'usable', 'usable_type', 'usable_id' );
}
}
Also, when I create an\App\Models\{-code-5}
from an\App\Models\
like this:
$person = \App\Models\::find( 1 );
$person->users()->create( [ ... ] );
The record in the database in the users table is like this:
+
Answer
But if I create the user from a\Modules\Contracts\Entities\Person
like this:
$person = \Modules\Contracts\Entities\Person::find( 1 );
$person->users()->create( [ ... ] );
The record in the database in the users table is like this:
{-code-33}
I wonder if theContractsServiceProvider
is somehow overriding the{-code-35}
and causing my relationship to fail. Any help would be appreciated.
Thanks
Answer
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: an exception occurred in the driver: could not find driver
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.