<file_sep><?php

namespace CristianVuolo\Paginas\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Pagina extends Model
{
    protected $table = 'cv_paginas';

    public function storePagina()
    {
        $request = request();
        $model = new $this;

        if($request->pagina_vinculada === '1'){
            $model->pagina = $request->pagina;
        }

        $model->nome = $request->nome;
        $model->titulo = ucwords($request->nome);
        $model->slug = slug($request->nome, 'cv_paginas');
        $model->descricao = $request->descricao;
        $model->resumo = $request->resumo;

        if($request->file) {
            $model->file = $this->upload($request->file('file'));
        }

        $model->conteudo = $request->conteudo;
        $model->views = 0;
        $model->save();
    }

    public function updatePagina($id)
    {
        $request = request();
        $model = $this->find($id);

        $model->nome = $request->nome;
        $model->titulo = ucwords($request->nome);

        $model->descricao = $request->descricao;
        $model->resumo = $request->resumo;

        if($request->file) {
            $model->file = $this->upload($request->file('file'));
        }

        if($request->pagina != ''){
            $model->pagina = $request->pagina;
        }
        $model->conteudo = $request->conteudo;
        $model->save();
    }

    public function deletePagina($id)
    {
        $pagina = $this->find($id);
        $pagina->delete();
    }

    public static function bySlug($slug)
    {
        $t = new self;
        return $t->where('slug', $slug)->first();
    }

    public function upload($file)
    {
        $file->move(public_path(config('CvConfigs.cv_page.upload.3.path')), $file->getClientOriginalName());
        return $file->getClientOriginalName();

    }
}
<file_sep><?php
return [
    'has_upload' => [999],
    'cannot_delete' => [999,1000],

    'upload' => [
        999 => [
            'path' => 'uploads/pages',
            'name' => 'Input Name',
            'link_name' => 'Btn Name',
        ],
    ],
];<file_sep><?php

Route::group(['prefix' => 'controle/paginas/', 'namespace' => 'CristianVuolo\\Paginas\\Controllers', 'middleware' => ['web', 'admin']], function () {

    Route::get('/', ['as' => 'admin.pagina.gerenciar', 'uses' => 'PaginaController@gerenciar']);
    Route::get('/novo', ['as' => 'admin.pagina.novo', 'uses' => 'PaginaController@novo']);
    Route::get('/alterar/{id}', ['as' => 'admin.pagina.alterar', 'uses' => 'PaginaController@alterar']);

    Route::post('/store', ['as' => 'admin.pagina.store', 'uses' => 'PaginaController@store']);
    Route::put('/update/{id}', ['as' => 'admin.pagina.update', 'uses' => 'PaginaController@update']);
    Route::get('/delete/{id}', ['as' => 'admin.pagina.delete', 'uses' => 'PaginaController@delete']);


});


Route::group(['prefix' => '/pagina/', 'namespace' => 'CristianVuolo\\Paginas\\Controllers', 'middleware' => ['web']], function () {
    Route::get('{slug}', ['as' => 'front.pagina.pagina', 'uses' => 'FrontPaginaController@pagina']);
});<file_sep><?php

namespace CristianVuolo\Paginas\Providers;


use Illuminate\Support\ServiceProvider;

class PaginasServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->publishes([__DIR__ . '/../../resources/publish/migrations/' => base_path('database/migrations')], 'migrations');
        $this->publishes([__DIR__ . '/../../resources/publish/views/' => base_path('resources/views/cv')], 'views');
        $this->publishes([__DIR__ . '/../../resources/publish/config/' => base_path('config\CvConfigs')], 'views');
        $this->loadViewsFrom(__DIR__ . '/../../resources/views/paginas', 'paginas');
        require __DIR__ . '/../../resources/routes/routes.php';
    }

    public function register()
    {

    }
}<file_sep><?php
namespace CristianVuolo\Paginas\Controllers;

use CristianVuolo\Paginas\Models\Pagina;

class FrontPaginaController extends Controller
{
    /**
     * @var Pagina
     */
    private $model;

    public function __construct(Pagina $model)
    {
        $this->model = $model;
    }

    public function pagina($slug)
    {
        
        $pagina = $this->model->whereSlug($slug);
        if ($pagina->count() === 1) {
            $pagina = $pagina->first();
            $pagina->increment('views');
            $view = [
                'title' => $pagina->titulo,
                'pagina' => $pagina,
            ];

            return view('cv.paginas.pagina', $view);
        }
        abort(404);
    }
}<file_sep><?php

namespace CristianVuolo\Paginas\Controllers;

use CristianVuolo\Paginas\Models\Pagina;

class PaginaController extends Controller
{
    /**
     * @var Pagina
     */
    private $model;

    public function __construct(Pagina $model)
    {
        $this->model = $model;
    }

    public function gerenciar()
    {
        $view = [
            'title' => 'Gerenciar Páginas',
            'paginas' => $this->model->where('block', '<>', '1')->get(),
        ];
        return view('paginas::paginas.gerenciar', $view);
    }

    public function novo()
    {
        $view = [
            'title' => 'Cadastrar Nova Página',
            'pagina' => new $this->model,
            'paginasNotRegistred' => [],
        ];

        return view('paginas::paginas.form', $view);
    }

    public function alterar($id)
    {
        $view = [
            'title' => 'Alterar Página',
            'pagina' => $this->model->find($id)
        ];

        return view('paginas::paginas.form', $view);
    }

    public function store()
    {
        $this->validatePagina();
        $this->model->storePagina();
        return redirect()->route('admin.pagina.gerenciar')->with('ok', 'Página Cadastrada');
    }

    public function update($id)
    {
        $this->validatePagina($id);
        $this->model->updatePagina($id);
        return redirect()->route('admin.pagina.gerenciar')->with('ok', 'Página Alterada');
    }

    public function delete($id)
    {
        if ($this->model->find($id)->block != 1) {
            $this->model->deletePagina($id);
            return redirect()->route('admin.pagina.gerenciar')->with('ok', 'Página Removida');
        }
        return redirect()->back()->with('erro', 'Essa Página não pode ser Removida');
    }

    private function validatePagina($id = null)
    {
        $rules = [
            'nome' => 'required',
            'descricao' => 'required',
            'conteudo' => 'required',
        ];


        $messages = [
            'required' => 'Campo Obrigatório'
        ];

        $this->validate(request(), $rules, $messages);
    }
}