PHP code example of kolirt / laravel-master-model

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

    

kolirt / laravel-master-model example snippets


use Kolirt\MasterModel\MasterModel;

class Item extends Model
{
    use MasterModel;
}

class Item extends Model
{
    use MasterModel;

    protected $fillable = [
        'image',
    ];
}


class ExampleController extends Controller
{
    public function index(Request $request, $id)
    {
        $data = $request->validate([
            'image' => '

class Item extends Model
{
    use MasterModel;

    protected $fillable = [
        'image',
    ];
    
    protected string $upload_model_folder = 'items';

    protected array $upload_folders = [
        'image' => 'image',
    ];

    protected array $upload_disks = [
        'image' => 'public'
    ];
}

class ExampleController extends Controller
{
    public function index($id)
    {
        $file1_url = 'https://png.pngtree.com/png-clipart/20230126/original/pngtree-fresh-red-apple-png-image_8930987.png';
        $response = \Illuminate\Support\Facades\Http::get($file1_url);

        $file2_url = 'https://cubanvr.com/wp-content/uploads/2023/07/ai-image-generators.webp';
        $client = new \GuzzleHttp\Client();
        $response2 = $client->get($file2_url);

        Item::create([
            'image' => $response,
            'image2' => $response2
        ]);
    }
}

$item = Item::query()->first();

$item->update([
    'image' => null
]);

$item = Item::query()->with(['phone', 'addresses'])->first();
/**
* All files in the model and in the loaded relations will be deleted
 */
$item->delete();

$item = Item::query()->first();

$item->update([
    'phone' => [ // hasOne, morphOne relation
        'number' => '1234567890'
    ]
]);

$item = Item::query()->first();

$item->update([
    'phone' => null // hasOne, morphOne relation
]);

$item = Item::query()->first();

$item->update([
    'phones' => [ // hasMany, morphMany relations
        [ // will be created
            'number' => '1234567890'
        ],
        [ // will be updated (id = 1)
            'id' => 1,
            'number' => '0987654321'
        ]
    ]
]);

$item = Item::query()->first();

$item->update([
    'phones' => [ // hasMany, morphMany relations
        'mode' => 'sync', // not specified relations will be deleted
        'value' => [
            [ // will be created
                'number' => '1234567890'
            ],
            [ // will be updated (id = 1)
                'id' => 1,
                'number' => '0987654321'
            ]
        ]
    ]
]);

$item = Item::query()->first();

$item->update([
    'categories' => [1, 2, 3] // belongsToMany relations
]);

$item->update([
    'categories' => [ // belongsToMany relation
        1 => ['name' => 'Category 1'], 
        2 => ['name' => 'Category 2'], 
        3 => ['name' => 'Category 3']
    ]
]);

$item = Item::query()->first();

$item->update([
    'categories' => [ // belongsToMany relation
        'mode' => 'sync', // not specified relations will be deleted
        'value' => [1, 2, 3]
    ]
]);

$item->update([
    'categories' => [ // belongsToMany relation
        'mode' => 'sync', // not specified relations will be deleted
        'value' => [
            1 => ['name' => 'Category 1'], 
            2 => ['name' => 'Category 2'], 
            3 => ['name' => 'Category 3']
        ]
    ]
]);

class FileController extends Controller
{

    public function index()
    {
        $item = Item::query()->first();
        return $item->responseFile('image');
    }

}
bash
php artisan master-model:install