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 :: Add ... if string is too long PHP 
Php :: laravel auth without vue or bootstrap 
Php :: php catch exception 
Php :: laravel db insert get last id 
Php :: Displaying the category name of a custom post type 
Php :: Merge Two Collection ( Laravel ) 
Php :: create laravel project with preferred version : 8 
Php :: php code to generate strong password 
Php :: php remove space from string 
Php :: php endwhile 
Php :: php do not refresh page after submit post 
Php :: Laravel Retrieve Session Data with default 
Php :: get_previous_posts_link add class wordpress 
Php :: add custom page to wordpress 
Php :: what is the difference between static and dynamic websites? 
Php :: string array to array in php 
Php :: php read text file into array 
Php :: how to check confirm password in php 
Php :: bycrypt password php 
Php :: laravel wherenotin 
Php :: encrypt & decrypt laravel 
Php :: set value in session php 
Php :: laravel custom validation exception 
Php :: add text next to price woocommerce 
Php :: laravel add column to table 
Php :: php set environment variable 
Php :: transient wordpress 
Php :: stdclass not found laravel 
Php :: ckeditor laravel 
Php :: php online editor 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =