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/be.naniguide.com/app/Model/SendingCreditPlan.php
<?php

namespace Acelle\Model;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Acelle\Library\Traits\HasUid;

class SendingCreditPlan extends Model
{
    use HasFactory;
    use HasUid;

    public static function newDefault()
    {
        $plan = new self();
        $plan->visibility = true;

        return $plan;
    }

    public function currency()
    {
        return $this->belongsTo('Acelle\Model\Currency');
    }

    public static function scopeSearch($query, $keyword)
    {
        $keyword = strtolower(trim($keyword));

        // search by keyword
        if ($keyword) {
            $query =  $query->whereRaw('LOWER(name) LIKE ? OR LOWER(description) LIKE ?', '%'.$keyword.'%', '%'.$keyword.'%');
        }
    }

    public function getPrice()
    {
        return $this->price;
    }

    public function fillParams($params)
    {
        $this->name = $params['name'] ?? null;
        $this->description = $params['description'] ?? null;
        $this->credits = $params['credits'] ?? null;
        $this->price = $params['price'] ?? null;
        $this->currency_id = $params['currency_id'] ?? null;
    }

    public function saveFromParams($params)
    {
        // fill
        $this->fillParams($params);

        $rules = [
            'name'   => ['required'],
            'description'   => ['required'],
            'credits'   => ['required'],
            'currency_id' => ['required'],
            'price' => ['required', 'min:0'],
        ];

        // validation
        $validator = \Validator::make($params, $rules);

        // check if has errors
        if ($validator->fails()) {
            return $validator;
        }

        // save to db
        $this->save();

        // return false
        return $validator;
    }
    public function visibilityOff()
    {
        $this->visibility = false;
        $this->save();
    }

    public function visibilityOn()
    {
        $this->visibility = true;
        $this->save();
    }

    public static function scopeVisible($query)
    {
        $query->where('visibility', true);
    }
}