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 = 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');