Add image to videos
This commit is contained in:
parent
45ac642c47
commit
e142f0b099
|
|
@ -44,6 +44,7 @@ class VideoCrudController extends CrudController
|
||||||
protected function setupListOperation()
|
protected function setupListOperation()
|
||||||
{
|
{
|
||||||
CRUD::column('title');
|
CRUD::column('title');
|
||||||
|
CRUD::column('image')->type('image');
|
||||||
CRUD::column('video');
|
CRUD::column('video');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -64,6 +65,7 @@ class VideoCrudController extends CrudController
|
||||||
CRUD::setValidation(VideoRequest::class);
|
CRUD::setValidation(VideoRequest::class);
|
||||||
|
|
||||||
CRUD::field('title');
|
CRUD::field('title');
|
||||||
|
CRUD::field('image')->type('image');
|
||||||
CRUD::field('video')->type('upload')->upload(true);
|
CRUD::field('video')->type('upload')->upload(true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ namespace App\Models;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;
|
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Intervention\Image\ImageManagerStatic as Image;
|
||||||
|
|
||||||
class Video extends Model
|
class Video extends Model
|
||||||
{
|
{
|
||||||
|
|
@ -23,6 +25,56 @@ class Video extends Model
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setImageAttribute($value)
|
||||||
|
{
|
||||||
|
$attribute_name = "image";
|
||||||
|
// or use your own disk, defined in config/filesystems.php
|
||||||
|
$disk = config('backpack.base.root_disk_name');
|
||||||
|
// destination path relative to the disk above
|
||||||
|
$destination_path = "public/uploads/videos/images";
|
||||||
|
|
||||||
|
// if the image was erased
|
||||||
|
if (empty($value)) {
|
||||||
|
// delete the image from disk
|
||||||
|
if (isset($this->{$attribute_name}) && !empty($this->{$attribute_name})) {
|
||||||
|
\Storage::disk($disk)->delete($this->{$attribute_name});
|
||||||
|
}
|
||||||
|
// set null on database column
|
||||||
|
$this->attributes[$attribute_name] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if a base64 was sent, store it in the db
|
||||||
|
if (Str::startsWith($value, 'data:image'))
|
||||||
|
{
|
||||||
|
// 0. Make the image
|
||||||
|
$image = \Image::make($value)->encode('jpg', 90);
|
||||||
|
|
||||||
|
// 1. Generate a filename.
|
||||||
|
$filename = md5($value.time()).'.jpg';
|
||||||
|
|
||||||
|
// 2. Store the image on disk.
|
||||||
|
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
|
||||||
|
|
||||||
|
// 3. Delete the previous image, if there was one.
|
||||||
|
if (isset($this->{$attribute_name}) && !empty($this->{$attribute_name})) {
|
||||||
|
\Storage::disk($disk)->delete($this->{$attribute_name});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Save the public path to the database
|
||||||
|
// but first, remove "public/" from the path, since we're pointing to it
|
||||||
|
// from the root folder; that way, what gets saved in the db
|
||||||
|
// is the public URL (everything that comes after the domain name)
|
||||||
|
$public_destination_path = Str::replaceFirst('public/', '', $destination_path);
|
||||||
|
$this->attributes[$attribute_name] = $public_destination_path.'/'.$filename;
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// crop image to 300x300
|
||||||
|
} elseif (!empty($value)) {
|
||||||
|
// if value isn't empty, but it's not an image, assume it's the model value for that attribute.
|
||||||
|
$this->attributes[$attribute_name] = $this->{$attribute_name};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function setVideoAttribute($value)
|
public function setVideoAttribute($value)
|
||||||
{
|
{
|
||||||
$attribute_name = "video";
|
$attribute_name = "video";
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ class VideoTransformer extends TransformerAbstract
|
||||||
return [
|
return [
|
||||||
'id' => $video->id,
|
'id' => $video->id,
|
||||||
'title' => $video->title,
|
'title' => $video->title,
|
||||||
|
'image' => url($video->image),
|
||||||
'video' => $file,
|
'video' => $file,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AddImageToVideos extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('videos', function (Blueprint $table) {
|
||||||
|
$table->after('title', function ($table) {
|
||||||
|
$table->text('image');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('videos', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('image');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue