-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add spatie media library to demo #456
Changes from 18 commits
9727f83
7f28b50
484506e
28d0911
c13f0dd
2ee8146
8b3c5ac
6a5f285
cc2d8c5
e593ec5
fb3ea41
aab2043
ec0b719
2319999
2dc38ce
4cfe51a
8fce79c
c95b3ec
5bdfe3e
d91123f
0a48a98
2886774
6e320e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,18 @@ | |
use App\Enums\ProductStatus; | ||
use Backpack\CRUD\app\Models\Traits\CrudTrait; | ||
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations; | ||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Spatie\MediaLibrary\HasMedia; | ||
use Spatie\MediaLibrary\InteractsWithMedia; | ||
|
||
class Product extends Model | ||
class Product extends Model implements HasMedia | ||
{ | ||
use CrudTrait; | ||
use HasTranslations; | ||
use HasFactory; | ||
use InteractsWithMedia; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
|
@@ -21,18 +25,80 @@ class Product extends Model | |
*/ | ||
|
||
protected $table = 'products'; | ||
|
||
protected $primaryKey = 'id'; | ||
|
||
public $timestamps = true; | ||
|
||
// protected $guarded = ['id']; | ||
protected $fillable = ['name', 'description', 'details', 'features', 'price', 'category_id', 'extras', 'status', 'condition']; | ||
protected $fillable = ['name', 'description', 'details', 'features', 'price', 'category_id', 'extras', 'status', 'condition', 'gallery', 'main_image', 'privacy_policy', 'specifications']; | ||
|
||
// protected $hidden = []; | ||
public $translatable = ['name', 'description', 'details', 'features', 'extras']; | ||
|
||
public $casts = [ | ||
'features' => 'object', | ||
'extra_features' => 'object', | ||
'status' => ProductStatus::class, | ||
'gallery' => 'json', | ||
'specifications' => 'array', | ||
]; | ||
|
||
public function mainImage(): Attribute | ||
{ | ||
return Attribute::make( | ||
set: function ($item) { | ||
if (app('env') === 'production') { | ||
return null; | ||
} | ||
|
||
return $item; | ||
}, | ||
); | ||
} | ||
|
||
public function privacyPolicy(): Attribute | ||
{ | ||
return Attribute::make( | ||
set: function ($item) { | ||
if (app('env') === 'production') { | ||
return null; | ||
} | ||
|
||
return $item; | ||
}, | ||
); | ||
} | ||
|
||
public function specifications(): Attribute | ||
{ | ||
return Attribute::make( | ||
set: function ($item) { | ||
if (app('env') === 'production') { | ||
return null; | ||
} | ||
|
||
return json_encode($item); | ||
}, | ||
); | ||
} | ||
Comment on lines
+73
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering... if the actual upload now... is being done with Uploaders, not Mutators... why are we using Mutators to catch and stop the upload process? Why don't we bind to all our uploaders... and prevent the upload process from happening... for ALL uploaders, if the env is production? Wouldn't that be cleaner, and more secure? That way nobody can introduce a vulnerability in our demo, all Uploaders are prevented from working in production. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe we can catch the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't urgent though. Just a thought. What I want right now is to get this merged and out of the way, ASAP. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can catch the That wouldn't prevent much, just the upload from happening, but anything you submit in the input would be saved to the database, except it didn't go through the upload process. At the moment we can't bind to our uploaders as they aren't resolved from the container, they are statically initialized. Probably it's like you said, it's more future proof, but at the moment it's not a requirement. |
||
|
||
public function gallery(): Attribute | ||
{ | ||
return Attribute::make( | ||
set: function ($item) { | ||
if (app('env') === 'production') { | ||
array_walk($item, function (&$row) { | ||
unset($row['gallery_image'], $row['gallery_image_drm'], $row['gallery_image_specifications'], $row['gallery_image_certificates']); | ||
|
||
return $row; | ||
}); | ||
} | ||
|
||
return json_encode($item); | ||
}, | ||
); | ||
pxpm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| FUNCTIONS | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with dropzone. Can't we bind on the
DropzoneOperation
trait, and prevent all dropzone uploads in production? It's too broad in a normal app, but in our case that's what we want, to prevent all uploads.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not urgent - just a thought. Right now I just want to merge this ASAP.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you thinking something like this:
And then in demo we use
App\Http\Controllers\Operations\DropzoneOperation
instead of the one in the package ?If you agree I can push this change.
Cheers