On laravel site with 3 related tables :
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
...
Schema::create('news', function (Blueprint $table) {
$table->id('id');
$table->string('title', 255);
$table->mediumText('content');
$table->string('content_shortly', 255)->nullable();
...
$table->foreignId('creator_id')->nullable()->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('RESTRICT');
...
Schema::create('complains', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->nullable()->references('id')->on('users')->onDelete('CASCADE');
$table->morphs('model');
$table->timestamp('created_at')->useCurrent();
...
Field news.creator_id is ref to users table
In app/Models/User.php I have using hasManyThrough method:
public function complainsOnNews()
{
return $this->hasManyThrough(
Complain::class,
News::class,
'creator_id', // Foreign key on the news table...
'model_id', // Foreign key on the Complains table...
'id', // Local key on the complains table...
'id' // Local key on the News table...
);
}
with request :
dd($this->profileUser->complainsOnNews);
I trace next sql :
SELECT `complains`.*, `news`.`creator_id` AS `laravel_through_key`
FROM `complains`
INNER JOIN `news` on `news`.`id` = `complains`.`model_id`
WHERE `news`.`creator_id` = 1
It is almost valid, but reference to complains.model_type missing some in some cases results would be wrong, it there some other models , not news
How that can be done ?
"laravel/framework": "^10.48.7",
Thanks in advance!