PHP code example of bardan-io / temp-media-for-laravel
1. Go to this page and download the library: Download bardan-io/temp-media-for-laravel 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/ */
bardan-io / temp-media-for-laravel example snippets
// 1. Upload images individually
$uploadResponse = app(TempMediaServiceInterface::class)->uploadTempMedia(
$uploadedFile,
session()->getId(),
auth()->id()
);
// 2. Create a product with temp media IDs
$product = Product::create([
'name' => 'My Product',
'description' => 'Product description',
// ... other fields
]);
// 3. Transfer temp media to product
$transferResult = $product->transferTempMedia(['temp-media-uuid-1', 'temp-media-uuid-2']);
use BardanIO\TempMedia\Traits\HandlesTempMedia;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Product extends Model implements HasMedia
{
use InteractsWithMedia;
use HandlesTempMedia;
// Transfer temp media to default collection
public function attachTempImages(array $tempMediaIds)
{
return $this->transferTempMedia($tempMediaIds, 'product_images');
}
}