PHP code example of webdev / laravel-loader

1. Go to this page and download the library: Download webdev/laravel-loader 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/ */

    

webdev / laravel-loader example snippets


composer 



namespace App\Models;

use WebDev\Loader\LoaderMacro;
use Illuminate\Database\Eloquent\Model;

abstract class BaseModel extends Model
{
    use LoaderMacro;
}

$product = Product::findOrFail($id);

return $product->loader(
       new ProductTranslate(new Language()),
       new ProductFeature(new Language()),
       'categories'
);

class ProductTranslate extends Loader
{

    protected $name = 'translate';

    // Translate
    public function init()
    {
        return ProductTranslate::where('product_id', $this->getModel()->id)
            ->where('language_id' => auth()->user()->language_id)
            ->firts()
            ->toArray();
    }
}

class ProductFeature extends Loader
{

    protected $name = 'features';

    // Features
    public function init()
    {
        return ProductFeature::where('product_id', $this->getModel()->id)->get()->toArray();
    }
}

class Language extends Loader
{

    protected $name = 'language';

    // Feature
    public function init()
    {
        return Language::findOrFail($this->getPrevious()->language_id)->toArray();
    }
}

array:2 [
  "translate" => array:5 [
    "id" => 4
    "name" => "Product name"
    "description" => "Description"
    "language_id" => 40
    "language" => array:6 [
      "id" => 40
      "code" => "en"
      "flag" => null
      "name" => "English"
      "native_name" => "English"
      "is_active" => 1
    ]
  ]
  "features" => array:2 [
    0 => array:2 [
      "id" => 23
      "name" => "Feature1"
      "feature_id" => 14
      "language_id" => 40
      "language" => array:6 [
        "id" => 40
        "code" => "en"
        "flag" => null
        "name" => "English"
        "native_name" => "English"
        "is_active" => 1
      ]
    ]
    1 => array:5 [
      "id" => 24
      "name" => "TH"
      "feature_id" => 14
      "language_id" => 156
      "language" => array:6 [
        "id" => 156
        "code" => "th"
        "flag" => null
        "name" => "Thai"
        "native_name" => "ไทย"
        "is_active" => 1
      ]
    ]
  ],
  "categories" => ...
]