laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

How to get attach attributes from belongsToMany relationship ?

Open phongjimmy opened this issue 2 years ago • 1 comments

  • Laravel-mongodb Version: 3.8.4
  • PHP Version: 7.4.12
  • Laravel: 8.55 (lastest)
  • Database Driver & Version: mongodb/mongodb 1.9

Description:

Steps to reproduce

Project Model

class Project extends Model
{
    use HasFactory;

    protected $guarded = ['_id'];

    public function socials(){
        return $this->belongsToMany(Social::class)->withPivot('link');
    }
}

Socials Model

class Social extends Model
{
    use HasFactory;

    protected $guarded = ['_id'];

    public function projects(){
        return $this->belongsToMany(Project::class);
    }
}

Tinker

$project = \App\Models\Project::create([
    'name' => 'Project Name',
    'website' => 'https://projectwebsite.com'
]);

$project->socials()->attach(' social _id ', ['link' => 'https://facebook.com/myproject']);

Result

$project = \App\Models\Project::first();
        foreach($project->socials as $social){
            var_dump($social->pivot->link); //null
            var_dump($social->pivot); //null
        }

Is this package support pivot attributes ?

phongjimmy avatar Sep 01 '21 14:09 phongjimmy

Any update for this feature?

zsinryu avatar Aug 08 '23 17:08 zsinryu