Repository.php 2.78 KB
Newer Older
1 2 3 4
<?php

namespace App\Repositories;

5 6
use Storage;

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
abstract class Repository
{
    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)
    {
        $request['userid_created'] = auth()->user()->id;
        return $this->model->create($request);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  Model  $model
     * @return \Illuminate\Http\Response
     */
    public function update($request, $model)
    {
        $request['userid_updated'] = auth()->user()->id;
        return $model->update($request);
    }

    /**
     * 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)
    {
        return $model->delete();
    }
Aan Choesni Herlingga committed
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    public function upload($name, $request, $tipe)
    {
        $setting = webprofilesetting();
        
        $cover = $request->file($tipe);
        if ($setting['external_storage'] == 1) {
            Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $name, file_get_contents($cover->getRealPath()));
        } else {
            Storage::disk('local')->put('public/' . $tipe . '/' . $name, file_get_contents($cover->getRealPath()));
        }
        return $name;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  Model  $model
     * @return \Illuminate\Http\Response
     */
    public function deletefile($model, $tipe)
Aan Choesni Herlingga committed
99
    {
100 101 102
        $setting = webprofilesetting();

        if ($model->$tipe) {
Aan Choesni Herlingga committed
103
            if ($setting['external_storage'] == 1) {
104
                Storage::disk('storage')->delete($setting['directory'] . '/' . $tipe . '/' . $model->$tipe);
Aan Choesni Herlingga committed
105
            } else {
106
                Storage::disk('local')->delete('public/' . $tipe . '/' . $model->$tipe);
Aan Choesni Herlingga committed
107 108 109
            }
        }
    }
110
}