PHP code example of zing / laravel-eloquent-images

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

    

zing / laravel-eloquent-images example snippets


use Zing\LaravelEloquentImages\Tests\Models\Product;
use Zing\LaravelEloquentImages\Image;

$product = Product::query()->first();
// Add image(s) to model
$product->attachImage("https://avatars.githubusercontent.com/u/26657141");
$product->attachImages([
    "https://avatars.githubusercontent.com/u/26657141",
    Image::query()->first()
]);
// Remove image(s) from model
$product->detachImage("https://avatars.githubusercontent.com/u/26657141");
$product->detachImages([
    "https://avatars.githubusercontent.com/u/26657141",
    Image::query()->first()
]);
// Reset images of model
$product->syncImages([
    "https://avatars.githubusercontent.com/u/26657141",
    Image::query()->first()
]);
// Get images of model
$product->images;
// Eager load images
$products = Product::query()->with('images')->withCount('images')->get();
$products->each(function (Product $product){
    $product->images->dump();
    $product->images_count;
});