Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pivot model translations #426

Open
Dezmonter opened this issue Aug 13, 2024 · 2 comments
Open

Pivot model translations #426

Dezmonter opened this issue Aug 13, 2024 · 2 comments
Labels

Comments

@Dezmonter
Copy link

I create models: Clinic, Service, ClinicService (pivot) + ClinicServiceTranslable

Clinic model

class Clinic extends Model
{
    use HasFactory, Translatable;

    protected $fillable = [
        'is_active',
    ];

    public $translatedAttributes = [
        'name',
    ];

    public function services(): belongsToMany
    {
        return $this->belongsToMany(Service::class)
        ->withPivot('price');
    }
}

Service Model

class Service extends Model
{
    use HasFactory, Translatable;

    protected $fillable = [
        'is_active'
    ];

    public $translatedAttributes = [
        'name',
    ];

    protected function casts(): array
    {
        return [
            'is_active'               => 'boolean',
        ];
    }

    public function clinics(): BelongsToMany
    {
        return $this->belongsToMany(Clinic::class)
        ->withPivot('price');
    }
}

ClinicService Model

class ClinicService extends Pivot implements TranslatableContract
{
    use HasFactory, Translatable;

    protected $translationForeignKey = 'clinic_service_id';

    public $incrementing = true;

    protected $fillable = [
        'clinic_id',
        'service_id',
        'price',
    ];

    public $translatedAttributes = [
        'comment',
    ];    
}

ClinicServiceTranslable Model

class ClinicServiceTranslation extends Model
{
    public $timestamps = false;

    protected $fillable = [
        'comment',
    ];
}

I write connections in the controller

 $clinic = Clinic::WithTranslation()->with(['services' => fn ($query) => $query->withTranslation()])->get();

 return response()->json($clinic);

and I get the response:

{
    "id": 8,
    "city_id": 2,
    "is_active": true,
    "name": "TestClinic",
    "translations": [
        {
            "clinic_id": 8,
            "locale": 'ru',
            "name": "TestClinic",
        }
    ],
    "services": [
        {
            "id": 80,
            "is_active": true,
            "name": "ServiceName",
            "pivot": {
                "clinic_id": 8,
                "service_id": 80,
                "price": "682.5000",
            },
            "translations": [
                {
                "service_id": 80,
                "locale": 'uk'',
                "name": "ServiceName",
                }
            ]
        },
    ]
}

Help me get comment from pivot translable model clinic_service_translation...
In the end I want to get this pivot responce:

"pivot": {
    "clinic_id": 8,
    "service_id": 80,
    "comment": TESTCOMMENT
    "price": "682.5000",
},
@Oleksandr-Moik
Copy link
Contributor

Does your pivod model have a primary key (id)? Can you show the migration for the ClinicService model?

@Gummibeer
Copy link
Member

I recommend you to use a regular model with belongsTo/hasMany relationships and utilize hasManyThrough if you want.
But pivots aren't made to handle advanced logic.

If you go with a dedicated ClinicServiceConfig or whatever you like as a regular model everything will just work smoothly. And by eager loading it also shouldn't have too much of an impact (if any) on performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants