EverydayPoemController.php 3.58 KB
<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\PoemTable;
use App\Admin\Repositories\EverydayPoem;
use App\Models\OnePoem;
use Carbon\Carbon;
use Dcat\Admin\Form;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class EverydayPoemController extends AdminController
{
    protected $title = '每日一言';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new EverydayPoem(), function (Grid $grid) {

            $grid->model()->with('poem');

            $grid->column('id')->sortable();
            $grid->column('date');
            $grid->column('poem.title');
            $grid->column('poem.author');
            $grid->column('poem.content')->limit(50);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new EverydayPoem(), function (Show $show) {

            $show->field('id');
            $show->field('date');
            $show->field('poem_id');
            $show->field('sn');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new EverydayPoem(['poem']), function (Form $form) {

            $form->display('id');
            $form->text('date');
            $form->text('should');
            $form->display('poem.title');
            $form->display('poem.author');
            $form->display('poem.content');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function create(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['create'] ?? trans('admin.create'))
            ->body(Form::make(new EverydayPoem(), function (Form $form) {
                $form->table('everyday','每日一言', function (NestedForm $table) {
                    $table->date('date');
                    $table->text('should');
                    $table->selectTable('poem_id')
                        ->title('一言诗词库')
                        ->from(PoemTable::make())
                        ->model(OnePoem::class, 'id', 'title');
                });
            }));
    }


    public function store()
    {
        $all = request()->all();

        try{
            $data = [];
            $now = Carbon::now();
            foreach ($all['everyday'] as $item) {
                if ($item['_remove_'] == 1) continue;
                $data[] = [
                    'date' => $item['date'],
                    'should' => $item['should'],
                    'poem_id' => $item['poem_id'],
                    'created_at' => $now,
                    'updated_at' => $now,
                ];
            }

            \App\Models\EverydayPoem::query()->insert($data);

        }catch (\Exception $exception){
            return $this->form()->response()->error($exception->getMessage());
        }


        return $this->form()->response()->refresh()->success(trans('admin.save_succeeded'));
    }
}