laravel-idea / plugin

Laravel Idea plugin for PhpStorm
https://laravel-idea.com/
166 stars 7 forks source link

[Feature Request]: Support for pivot values in models #611

Open abenerd opened 2 years ago

abenerd commented 2 years ago

Feature Description

<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    public function companies(): BelongsToMany
    {
        return $this->belongsToMany(Company::class)->withPivot(['role']);
    }
}
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Company extends Model
{
    public function members(): BelongsToMany
    {
        return $this->belongsToMany(User::class)->withPivot(['role']);
    }
}

Say you have a relationship between two tables like these, when I now find a user via the company or vise versa I would love to be able to autocomplete the pivot values.


Company::query()->first()->members()->find(3)->pivot->role;

Ist it possible to autocomplete the last part, the ->pivot->role part when generating the helper code for the models?

Azeirah commented 1 year ago

Similarly, I have the following pivot relation

class Salesarea { 
  // ...  
  public function scheduled_homescreen_videos(): MorphToMany
  {
    return $this->morphToMany(TimeSchedule::class, "schedulable")
      ->withPivot("id")
      ->using(SchedulableHomescreenVideo::class)
      ->as("schedulable");
  }
}
class SchedulableHomescreenVideo extends MorphPivot implements HasMedia
{
  use InteractsWithMedia;
  protected $table = "schedulables";
}

I use it in this way, for example:

$time_schedule->schedulable->addMedia($file)->toMediaCollection();

Laravel idea doesn't know that $time_schedule->schedulable is an instance of SchedulableHomescreenVideo here.