'boolean', // Cast to boolean. 'active' => 'boolean', // Cast to boolean. 'show' => 'boolean', // Cast to boolean. 'for_barayand' => 'boolean', // Cast to boolean. 'open_sell' => 'boolean', // Cast to boolean. 'auto_sell' => 'boolean', // Cast to boolean (you mentioned this in your casts but it's missing in `$fillable`). 'open_buy' => 'boolean', // Cast to boolean. 'auto_buy' => 'boolean', // Cast to boolean. 'price' => 'int', // Cast to float. 'percent_fee_sell' => 'float', // Cast to float. 'percent_fee_buy' => 'float', // Cast to float. 'cash_fee_buy' => 'int', // Cast to int or decimal if needed 'cash_fee_sell' => 'int', // Cast to int or decimal if needed ]; static $METAL_ID = [ 1=>'طلا', 2=>'نقره', ]; public function wallet() { return $this->hasOne(Wallet::class); } public function walletMy() { return $this->hasOne(Wallet::class)->where('owner',auth()->user()->id); } public function file(){ return $this->belongsTo(File::class); } public function price(){ return $this->hasOne(Price::class)->orderBy('id','desc'); } public function getPrices() { $now = Carbon::now(); $lastMonth = Carbon::now()->subMonth(); $subQuery = DB::table('prices') ->select('product_id', 'date_at', 'price') ->whereBetween('date_at', [$lastMonth, $now]) ->where('product_id', $this->id) ->orderBy('date_at', 'desc') ->distinct('date_at') ->groupBy('date_at', 'product_id', 'price'); return DB::table(DB::raw("({$subQuery->toSql()}) as sub")) ->mergeBindings($subQuery->getQuery()) ->get(['product_id', 'date_at', 'price']); } public function prices(){ $now = Carbon::now(); $lastMonth = Carbon::now()->subMonth(); $data = []; $datas= $this->hasMany(Price::class, 'product_id') ->whereBetween('date_at', [$lastMonth, $now]) ->distinct('date_at') ->select(['product_id', 'date_at', 'difference', 'price']); return $datas; } public function default(){ return $this->hasMany(ProductCredit::class); } public function sub(){ return $this->hasOne(PackageProduct::class)->where('is_base',true); } // public function wallet(){ // return $this->hasOne(Wallet::class); // } public function transaction(){ return $this->hasMany(Transaction::class); } public function transactionAgentPanel(){ return $this->hasMany(Transaction::class)->where('note','برداشت بابت فروش')->orwhere('note','واریز بابت خرید')->orwhere('note','سند ورود طلا'); } }