HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //home/xedaptot/iphim.naniguide.com/app/Http/Controllers/AdminTitleTagsController.php
<?php

namespace App\Http\Controllers;

use App\Models\Title;
use Common\Core\BaseController;
use Common\Database\Datasource\Datasource;

class AdminTitleTagsController extends BaseController
{
    public function index(string $tagType)
    {
        $this->authorize('index', Title::class);

        $builder = app(modelTypeToNamespace($tagType))->newQuery();

        $dataSource = new Datasource($builder, request()->all());

        $pagination = $dataSource->paginate();

        return $this->success(['pagination' => $pagination]);
    }

    public function store(string $type) {
        $this->authorize('store', Title::class);

        $data = $this->validate(request(), [
            'name' => 'required|string',
            'display_name' => 'string',
        ]);

        $tag = app(modelTypeToNamespace($type))->create($data);

        return $this->success(['tag' => $tag]);
    }

    public function update(string $type, int $tagId) {
        $this->authorize('update', Title::class);

        $data = $this->validate(request(), [
            'name' => 'string',
            'display_name' => 'string',
        ]);

        $tag = app(modelTypeToNamespace($type))->findOrFail($tagId);

        $tag->update($data);

        return $this->success(['tag' => $tag]);
    }

    public function destroy($type, string $ids)
    {
        $tagIds = explode(',', $ids);
        $this->authorize('destroy', Title::class);

        foreach ($tagIds as $tagId) {
            $tag = app(modelTypeToNamespace($type))->findOrFail($tagId);
            $tag->titles()->detach();
            $tag->delete();
        }

        return $this->success();
    }
}