staudenmeir / laravel-adjacency-list

Recursive Laravel Eloquent relationships with CTEs
MIT License
1.37k stars 111 forks source link

Support for spatie/laravel-translatable #271

Open MattLoyeD opened 6 days ago

MattLoyeD commented 6 days ago

Hi,

I do not get translated fields when using : Category::tree(3)->get()

Any idea on how to solve this ? I'll be glad to do a PR etc, but I did not succeed so far on getting it work along your nice package.

I tried this below but couldn't get it work as well :

public function parseCategoryTranslations(array|Collection $categories): array|Collection
    {

            // Foreach categories, parse field with translations, it's probably a json in name and description
            // Do it recuirsively for all children
            foreach ($categories as $k => $category) {

                // reparse catergory from db
                $new = Category::find($category->id);
                $category->name = $new->name;
                $category->description = $new->description;

                // Recursive call for descendants
                // ----------------------------
                if ($category->descendants->count() > 0) {
                    $category->descendants = $this->parseCategoryTranslations($category->descendants);
                }

                $categories[$k] = $category;
            }
            return $categories;

    }

See : https://spatie.be/docs/laravel-translatable/v6/basic-usage/getting-and-settings-translations

staudenmeir commented 6 days ago

Hi @MattLoyeD, What does your Category model look like?

MattLoyeD commented 5 days ago

Thanks for your kind answer, here is the Model :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use Spatie\Translatable\HasTranslations;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

class Category extends Model
{
    use HasFactory;
    use HasUuids;
    use LogsActivity;
    use HasTranslations;
    use HasRecursiveRelationships;

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'description',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'hash' => 'string',
    ];

    protected $translatable = [
        'name',
        'description'
    ];

    protected $with = [
        'descendants',
    ];

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['name']);
    } 
}
staudenmeir commented 5 days ago

I do not get translated fields when using : Category::tree(3)->get()

Do the translations work correctly with a "normal" query like Category::all()?