Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel model tree

//Function
public function getTree()
    {
        return AttributeGroupModel::query()->with('attributes')->get()->transform(function ($group) {
            return [
                'id' => $group->id,
                'name' => AdminService::trans($group->name),
                'values' => $group->attributes->map(function ($attribute) use ($group) {
                    return [
                        'id' => $attribute->id,
                        'name' => AdminService::trans($attribute->name),
                        'group_id' => $group->id,
                    ];
                })
            ];
        });
    }

//Model

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
use SpatieTranslatableHasTranslations;

class AttributeGroupModel extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = "attribute_groups";


    protected $fillable = [
        "id",
        "name",
        "sort_order"
    ];

    protected $casts = [
        'name' => 'array',
    ];

    public function attributes()
    {
        return $this->hasMany(AttributeModel::class, 'attribute_group_id', 'id');
    }

}
Comment

laravel tree

 public function autocomplete(array $tree, $parent_id = 0, $parent_name = null)
    {
        $result = [];
        foreach ($tree as $item) {
            if (!empty($parent_name)) {
                $name = $parent_name . ' > ' . AdminService::trans($item['title']);
            } else {
                $name = AdminService::trans($item['title']);
            }
            $result[] = [
                'id' => $item['id'],
                'title' => $name,
            ];
            if (count($item['children']) > 0) {
                $result = array_merge($result, $this->autocomplete($item['children'], $parent_id, $name));
            }
        }
        return $result;
    }
Comment

PREVIOUS NEXT
Code Example
Php :: laravel array remove key 
Php :: create user with tinker php laravel 
Php :: raw query in laravel with parameters 
Php :: factorial program in php using recursive function 
Php :: Call to undefined method IlluminateSessionStore::set() 
Php :: php foreach array 
Php :: php convert special characters to normal 
Php :: format datetime ISO php 
Php :: Auth::routes(); why display error in route laravel 8 
Php :: where laravel function 
Php :: nav active in laravel 
Php :: read global laravel request() 
Php :: array_key_exists vs isset 
Php :: date to string in php 
Php :: try catch in laravel 
Php :: format date in laravel using carbon 
Php :: eliminar ultimo caracter string php 
Php :: nl2br php 
Php :: laravel load view in variable 
Php :: php empty 
Php :: laravel query builder sum 
Php :: php array size 
Php :: how to show validation error in laravel 8 
Php :: laravel seed fresh 
Php :: carbon parse timestamp 
Php :: php round() function 
Php :: laravel make model with migration 
Php :: how to save file in storage folder in laravel 
Php :: To find out where your php.ini is located 
Php :: minus 1 year php 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =