PHP code example of i-ismail / laravel-file-upload

1. Go to this page and download the library: Download i-ismail/laravel-file-upload 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/ */

    

i-ismail / laravel-file-upload example snippets


$post = new Post();
//...
$post->image = FileUpload::make(request('image'))->store();

$post->save();

$post = new Post();
//...
$post->image = FileUpload::make(request('image'))
->store('posts');

$post->save();

$post = new Post();
//...
$post->image = FileUpload::make(request('image'))
->disk('s3')
->store('posts');

$post->save();

$post = new Post();
//...
$post->image = FileUpload::make(request('image'))
->fileName('my-image')
->store('posts');

$post->save();

$post = new Post();
//...
$post->image = FileUpload::make(request('image'))
->extension('jpg')
->store('posts');

$post->save();

$post = new Post();
//...
$post->image = FileUpload::make(request('image'))
                    ->resize(400, 400)
                    ->crop(100, 100, 25, 25)
                    ->store('posts');
$post->save();

$post = Post::find(1);
//...
$post->image = FileUpload::delete($post->image);

$post = Post::find(1);
//...
$post->image = FileUpload::make(request('image'))
                    ->delete($post->image)
                    ->store('posts');
$post->save();

$post = new Post();
//...
$fileUpload = FileUpload::make(request('image')); 
$post->image = $fileUpload->store('posts');
$filePath = $fileUpload->getFilePath();

$post->save();

$post = Post::create([
        //...
]);

//In Post Model
public function setImageAttribute($image)
{
    $this->attributes['image'] = FileUpload::make($image)->store('posts');
}

//In Post Model
public function getImgAttribute()
{
    return $this->image ? asset('storage/'. $this->image) : asset('images/post.jpg');
}

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class Post extends Model  implements HasMedia
{
    use InteractsWithMedia;
    ...
}

$post = new Post();
//...
$post->save();
MediaUpload::make(request('images'))->setModel($post)->store();


$post = new Post();
//...
$post->save();

MediaUpload::make(request('images'))
->setModel($post)
->store('posts');


$post = new Post();
//...
$post->save();

MediaUpload::make(request('images'))
->setModel($post)
->disk('s3')
->store('posts');


$post = new Post();
//...
$post->save();
MediaUpload::make(request('images'))
    ->resize(500, 200)
    ->crop(100, 100, 25, 25)
    ->setModel($post)
    ->store('posts');

$post = new Post();
//...
$post->save();
MediaUpload::make(request('images')) 
    ->resize(500, 200)
    ->setModel($post)
    ->usingName('my-image-name')
    ->withCustomProperties([
        'primaryColor' => 'red',
        'image-code'  => '12458558',
    ])
    ->store('posts');

$post->getMedia('posts'); // posts is collection name.

$post->getFirstMedia('posts');

$post->getFirstMediaUrl('posts');

$mediaItem->delete();

$post->delete(); // all associated files will be deleted as well.

$post->deletePreservingMedia(); // all associated files will be preserved.
bach
php artisan vendor:publish --tag=file-upload-config
bach
php artisan storage:link
bach
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
bach
php artisan migrate
bach
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"