PHP code example of orlyapps / nova-belongsto-depend

1. Go to this page and download the library: Download orlyapps/nova-belongsto-depend library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

orlyapps / nova-belongsto-depend example snippets



use Orlyapps\NovaBelongsToDepend\NovaBelongsToDepend;

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        Text::make('Name')->rules('pp\Company::all()),
        NovaBelongsToDepend::make('Department')
            ->placeholder('Optional Placeholder') // Add this just if you want to customize the placeholder
            ->optionsResolve(function ($company) {
                // Reduce the amount of unnecessary data sent
                return $company->departments()->get(['id','name']);
            })
            ->dependsOn('Company'),
        NovaBelongsToDepend::make('Location')
            ->placeholder('Optional Placeholder') // Add this just if you want to customize the placeholder
            ->optionsResolve(function ($company) {
                // Reduce the amount of unnecessary data sent
                return $company->locations()->get(['id','name']);
            })
            ->fallback(
                Text::make('Location Name')->rules('

    /**
     * @return mixed
     */
    public function getNameAttribute()
    {
        return $this->getTranslations('name')[app()->getLocale()];
    }

class Company extends Resource
{
  public static $with = [];
}

class Department extends Resource
{
  public static $with = ['company'];
}

class Location extends Resource
{
  public static $with = ['department', 'company'];
}

NovaBelongsToDepend::make('Company')
    ->options(Cache::remember(
        'companies', 
        60, 
        function () { 
            return Company::all(); 
        }
    )),
NovaBelongsToDepend::make('Department')
    ->dependsOn('Company')
    ->optionsResolve(function($company) { 
        return $company->departments;
    })

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        Text::make('Name')->rules('    NovaBelongsToDepend::make('Article')
            ->optionsResolve(function ($warehouse) {
                return $warehouse->articles;
            })
            ->dependsOn('Warehouse')
            ->rules(' ->rules('

->dependsOn('classification', 'brand')

public function fields(Request $request)
{
    return [
        ID::make(__('ID'), 'id')->sortable(),
        NovaBelongsToDepend::make('Classification', 'classification')
            ->options(\App\Models\Classification::all()),
        NovaBelongsToDepend::make('Brand', 'brand')
            ->optionsResolve(function ($classification) {
                return $classification->brands()->get(['brands.id', 'brands.name']);
            })
            ->dependsOn('classification'),
        NovaBelongsToDepend::make('Model', 'model', VehicleModel::class)
            ->optionsResolve(function ($depends) {
                return \App\Models\VehicleModel::query()
                    ->where('classification_id', $depends->classification->id)
                    ->where('brand_id', $depends->brand->id)
                    ->get();
            })
            ->dependsOn('classification', 'brand'),
    ];
}