%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/diaspora/api/app/Models/
Upload File :
Create Path :
Current File : /var/www/html/diaspora/api/app/Models/MyCountryCategory.php

<?php

namespace App\Models;

use Dimsav\Translatable\Translatable;
use Illuminate\Support\Facades\DB;

/**
 * @property integer id
 * @property string order
 * @property string title
 * @property string description
 * @property boolean is_visible
 * @property mixed sections
 */
class MyCountryCategory extends BaseModel
{
    use Translatable;

    public $translatedAttributes = [
        'title',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'order',
        'title',
    ];

    protected $casts = [
        'id'    => 'integer',
        'order' => 'integer',
        'title' => 'string',
    ];

    public function sections()
    {
        return $this->hasMany(MyCountrySection::class, 'category_id');
    }

    public function moveUp()
    {
        DB::beginTransaction();
        try {
            $oldOrder = (int)$this->order;
            $newOrder = $oldOrder - 1;
            /** @var Section $section */
            $section = self::query()
                           ->where('order', $newOrder)
                           ->first();
            if ($section) {
                $section->order = 999;
                $section->save();
                $this->order = $newOrder;
                $this->save();
                $section->order = $oldOrder;
                $section->save();
                DB::commit();
            }
            return $this;
        } catch (\Exception $e) {
            DB::rollBack();
            return $this;
        }
    }

    public function moveDown()
    {
        DB::beginTransaction();
        try {
            $oldOrder = (int)$this->order;
            $newOrder = $oldOrder + 1;
            /** @var MyCountrySection $section */
            $section = self::query()
                           ->where('order', $newOrder)
                           ->first();
            if ($section) {
                $section->order = 999;
                $section->save();
                $this->order = $newOrder;
                $this->save();
                $section->order = $oldOrder;
                $section->save();
                DB::commit();
            }
            return $this;
        } catch (\Exception $e) {
            DB::rollBack();
            return $this;
        }
    }

    public function canBeEnabled()
    {
        return $this->translations()
                    ->count() === 2;
    }

    public function getRelationKey()
    {
        return 'category_id';
    }

}



Zerion Mini Shell 1.0