Skip to content
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

added backend product image upload #205

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion resources/css/app.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@import '~easymde/dist/easymde.min.css';

.avored-input {
@apply w-full px-3 py-2 ring-gray-300 ring-1 rounded shadow-sm appearance-none text-gray-700;
Expand Down
3 changes: 2 additions & 1 deletion resources/lang/en/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,6 @@
'disabled' => 'Disabled',
'customer_reset_password_link' => 'Customer Reset Password Link',
'success_sent_password_reset_email_message' => 'Successfully Password Reset Email has been sent to customer',
'images' => 'Images'
'images' => 'Images',
'product_success_upload_message' => 'Product Document Uploaded successfully'
];
33 changes: 30 additions & 3 deletions resources/views/catalog/product/cards/images.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<div x-data="fileupload" x-init="fileuploadinit">
<label
class="flex bg-white justify-center w-full h-32 p-4 transition border-2 border-gray-300 border-dashed rounded-md appearance-none cursor-pointer hover:border-gray-400 focus:outline-none">
<span class="flex items-center space-x-2">
Expand All @@ -12,6 +12,33 @@ class="flex bg-white justify-center w-full h-32 p-4 transition border-2 border-g
<span class="text-blue-600 underline">browse</span>
</span>
</span>
<input type="file" name="file_upload" class="hidden">
<input type="file" x-on:change="fileOnChange" name="file_upload" class="hidden">
</label>
</div>
</div>

<script>
document.addEventListener('alpine:init', () => {
Alpine.data('fileupload', () => ({
open: false,

fileuploadinit() {
console.log('init')
},
fileOnChange (e) {
console.log(e.target.files)

var formData = new FormData();
formData.append("image", e.target.files[0]);
axios.post('/admin/product-image/10981be7-ac5b-485a-8ef6-de03584be21e', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(({data}) => {


})
}
}))
})
</script>

1 change: 1 addition & 0 deletions resources/views/system/components/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<title>@yield('meta_title', 'AvoRed E commerce')</title>

<link rel="stylesheet" href="{{ asset('vendor/avored/css/app.css') }}"></link>
<link rel="stylesheet" href="https://unpkg.com/easymde/dist/easymde.min.css">

<livewire:styles />
<livewire:scripts />
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
Route::resource('attribute', AttributeController::class);
Route::resource('product', ProductController::class);

Route::post('product-image/{product}', [ProductController::class, 'upload']);


/***************** USER ROUTES *****************/
Route::resource('staff', StaffController::class);
Expand Down
28 changes: 28 additions & 0 deletions src/Catalog/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AvoRed\Framework\Database\Models\Product;
use AvoRed\Framework\Tab\Tab;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class ProductController extends Controller
Expand Down Expand Up @@ -128,4 +129,31 @@ public function destroy(Product $product)
'message' => __('avored::system.success_delete_message', ['product' => __('avored::system.product')])
]);
}

/**
* Upload a product image.
*
* @param Product $product
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function upload(Product $product, Request $request)
{
$image = $request->file('image');

$path = $image->store('uploads/catalog', 'public');
$product->document()->create([
'path' => $path,
'mime_type' => $image->getClientMimeType(),
'size' => $image->getSize(),
'origional_name' => $image->getClientOriginalName(),
]);


return new JsonResponse([
'success' => true,
'data' => $product->document,
'message' => __('avored::system.product_success_upload_message', ['product' => __('avored::system.product')])
]);
}
}
2 changes: 1 addition & 1 deletion src/Database/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function categories()
*/
public function document()
{
return $this->morphOne(Document::class, 'documentable');
return $this->morphMany(Document::class, 'documentable');
}

/**
Expand Down