Commit 9692f6b0 by Aan Choesni Herlingga

Storage Repository update

parent 081a8ca9
......@@ -48,7 +48,7 @@ class SliderController extends Controller
*/
public function store(Request $request)
{
$this->repo->store($request);
$this->repo->store($request, 'slider');
return redirect()->route('sliders.index');
}
......@@ -91,7 +91,7 @@ class SliderController extends Controller
public function update(Request $request, $id)
{
$slider = $this->repo->findId($id);
$edit = $this->repo->update($request, $slider);
$edit = $this->repo->update($request, $slider, 'slider');
return redirect()->route('sliders.index');
}
......@@ -105,7 +105,7 @@ class SliderController extends Controller
public function destroy($id)
{
$data = $this->repo->findId($id);
$this->repo->destroy($data);
$this->repo->destroy($data, 'slider');
return response()->json(['done']);
}
......
<?php
namespace App\Repositories;
use Illuminate\Support\Str;
use Storage;
abstract class StorageRepository
{
protected $model;
abstract public function get();
/**
* Display specified resource.
*
* @param varchar $with
* @param uuid $id
* @return \Illuminate\Http\Response
*/
public function findId($id = null, $with = null)
{
return $this->model
->when($with, function ($query) use ($with) {
return $query->with($with);
})
->when($id, function ($query) use ($id) {
return $query->where('id', $id);
})
->first();
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store($request, $tipe)
{
$setting = webprofilesetting();
$data = $request->except('_token');
$data['id'] = Str::uuid()->toString();
if ($request->hasFile($tipe)) {
$cover = $request->file($tipe);
$extension = $cover->guessClientExtension();
$filename = $data['id'] . '.' . $extension;
if ($setting['external_storage'] == 1) {
Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
} else {
Storage::disk('local')->put('public/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
}
$data[$tipe] = $filename;
}
$request['userid_created'] = auth()->user()->id;
return $this->model->create($data);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param Model $model
* @return \Illuminate\Http\Response
*/
public function update($request, $model, $tipe)
{
$setting = webprofilesetting();
$data = $request->except('_token');
if ($request->hasFile($tipe)) {
$cover = $request->file($tipe);
$extension = $cover->guessClientExtension();
$filename = $model->id . '.' . $extension;
if ($setting['external_storage'] == 1) {
Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
} else {
Storage::disk('local')->put('public/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
}
$data[$tipe] = $filename;
}
$data['userid_updated'] = auth()->user()->id;
return $model->update($data);
}
/**
* Show the specified resource in storage.
*
* @param uuid $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return $this->model->where('user_id', $id)->first();
}
/**
* Remove the specified resource from storage.
*
* @param Model $model
* @return \Illuminate\Http\Response
*/
public function destroy($model, $tipe)
{
$setting = webprofilesetting();
if ($model->images) {
if ($setting['external_storage'] == 1) {
Storage::disk('storage')->delete($setting['directory'] . '/' . $tipe . '/' . $model->images);
} else {
Storage::disk('local')->delete('public/' . $tipe . '/' . $model->images);
}
}
$model->delete($model->id);
}
}
......@@ -3,12 +3,12 @@
namespace App\Repositories\Webprofile;
use App\Models\Webprofile\Slider;
use App\Repositories\Repository;
use App\Repositories\StorageRepository;
use Illuminate\Support\Str;
use DataTables;
use Storage;
class SliderRepository extends Repository
class SliderRepository extends StorageRepository
{
protected $model;
......@@ -32,91 +32,6 @@ class SliderRepository extends Repository
->get();
}
/**
* Custom Store a newly created resource in Slider.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store($request)
{
$setting = webprofilesetting();
$data = $request->except('_token');
$data['id'] = Str::uuid()->toString();
if ($request->hasFile('images')) {
$cover = $request->file('images');
$extension = $cover->guessClientExtension();
$filename = $data['id'] . '.' . $extension;
if ($setting['external_storage'] == 1) {
Storage::disk('storage')->put($setting['directory'] . '/slider/' . $filename, file_get_contents($cover->getRealPath()));
} else {
Storage::disk('local')->put('public/slider/' . $filename, file_get_contents($cover->getRealPath()));
}
$data['images'] = $filename;
}
$request['userid_created'] = auth()->user()->id;
return $this->model->create($data);
}
/**
* Custom Update a resource in Slider.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update($request, $sModel)
{
$setting = webprofilesetting();
$data = $request->except('_token');
if ($request->hasFile('images')) {
$cover = $request->file('images');
$extension = $cover->guessClientExtension();
$filename = $sModel->id . '.' . $extension;
if ($setting['external_storage'] == 1) {
Storage::disk('storage')->put($setting['directory'] . '/slider/' . $filename, file_get_contents($cover->getRealPath()));
} else {
Storage::disk('local')->put('public/slider/' . $filename, file_get_contents($cover->getRealPath()));
}
$data['images'] = $filename;
}
$data['userid_updated'] = auth()->user()->id;
return $sModel->update($data);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Slider $slider
* @return \Illuminate\Http\Response
*/
public function destroy($sModel)
{
$setting = webprofilesetting();
try {
if ($sModel->images) {
if ($setting['external_storage'] == 1) {
Storage::disk('storage')->delete($setting['directory'] . '/slider/' . $sModel->images);
} else {
Storage::disk('local')->delete('public/slider/' . $sModel->images);
}
}
$sModel->delete($sModel->id);
return redirect()->route('sliders.index');
} catch (\Exception $id) {
return redirect()->route('sliders.index');
}
}
public function datatable($data)
{
$setting = webprofilesetting();
......@@ -134,9 +49,9 @@ class SliderRepository extends Repository
})
->addColumn('slider', function ($row) use ($setting) {
if ($setting['external_storage'] == 1) {
$str = '<img src="'. $setting['url_static'] . '/' . $setting['directory'] . '/slider/' . $row->images . '" height="100" width="auto">';
$str = '<img src="'. $setting['url_static'] . '/' . $setting['directory'] . '/slider/' . $row->slider . '" height="100" width="auto">';
} else {
$str = '<img src="'. url('/storage/slider/' . $row->images) . '" height="100" width="auto">';
$str = '<img src="'. url('/storage/slider/' . $row->slider) . '" height="100" width="auto">';
}
return $str;
......
......@@ -16,7 +16,7 @@ class CreateSlidersTable extends Migration
Schema::create('swp_sliders', function (Blueprint $table) {
$table->string('id', 36)->primary();
$table->string('title');
$table->string('images')->nullable();
$table->string('slider')->nullable();
$table->text('content')->nullable();
$table->boolean('is_active')->nullable();
$table->string('userid_created', 36)->nullable();
......
......@@ -36,7 +36,7 @@
<img id="uploadPreview" style="width: 500px; height: 100%;" src="{{URL::to('https://statik.unesa.ac.id/perpus_konten_statik/uploads/slider/slider.png')}}"/><br>
</div>
<div class="form-group">
{{ Form::file('images', ['class'=>'fileinput btn-danger', 'id'=>'uploadImage', 'data-filename-placement'=>'inside', 'title'=>trans('label.upload'), 'onchange'=>'PreviewImage();', 'accept'=>'image/jpeg,image/png']) }}
{{ Form::file('slider', ['class'=>'fileinput btn-danger', 'id'=>'uploadImage', 'data-filename-placement'=>'inside', 'title'=>trans('label.upload'), 'onchange'=>'PreviewImage();', 'accept'=>'image/jpeg,image/png']) }}
</div>
</center>
</div>
......
......@@ -46,7 +46,7 @@
@endif
</div>
<div class="form-group">
{{ Form::file('images', array('class'=>'fileinput btn-danger', 'id'=>'uploadImage', 'data-filename-placement'=>'inside', 'title'=>'Upload', 'onchange'=>'PreviewImage();', 'accept'=>'image/jpeg,image/png')) }}
{{ Form::file('slider', array('class'=>'fileinput btn-danger', 'id'=>'uploadImage', 'data-filename-placement'=>'inside', 'title'=>'Upload', 'onchange'=>'PreviewImage();', 'accept'=>'image/jpeg,image/png')) }}
</div>
</center>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment