我正在构建资产管理系统,并且试图检查购买日期是否在某个财政年度之间,因此我可以更新资产表中的year列,我在控制器中有以下代码:
//year 2016
$from2016 = date('2016-04-06');
$to2016 = date('2017-04-05');
$assets = Asset::all();
foreach($assets as $asset)
{
if ($asset->purchase_date->between( $from2016,$to2016) )
{
$now = Carbon::now();
if ($now->between( $from2018,$to2018) )
{
$asset->year= 2;
$asset->save();
}
else if ($now->between( $from2019,$to2019) )
{
$asset->year=3;
$asset->save();
}
else if ($now->between( $from2020,$to2020) )
{
$asset->year=4;
$asset->save();
}
else if ($now->between( $from2021,$to2021) )
{
$asset->year=5;
$asset->save();
}
}
}
这是我的模特
使用Illuminate \ Database \ Eloquent \ Model; 使用Carbon \ Carbon;
/**
* Class Asset
*
* @package App
* @property string $title
* @property string $serial_number
* @property string $barcode
* @property string $photo1
* @property string $category
* @property string $status
* @property string $location
* @property string $assigned_user
* @property string $purchase_date
* @property string $vendor
* @property decimal $purchase_price
* @property string $warranty
* @property string $assigned_date
* @property text $notes
*/
class Asset extends Model
{
protected $fillable = ['title', 'serial_number', 'barcode', 'photo1',
'purchase_date', 'purchase_price', 'warranty', 'assigned_date', 'notes',
'category_id', 'status_id', 'location_id', 'assigned_user_id',
'vendor_id'];
protected $hidden = [];
protected $dates = ['purchase_date'];
public static function boot()
{
parent::boot();
Asset::observe(new \App\Observers\UserActionsObserver);
Asset::observe(new \App\Observers\AssetsHistoryObserver);
}
/**
* Set to null if empty
* @param $input
*/
public function setCategoryIdAttribute($input)
{
$this->attributes['category_id'] = $input ? $input : null;
}
/**
* Set to null if empty
* @param $input
*/
public function setStatusIdAttribute($input)
{
$this->attributes['status_id'] = $input ? $input : null;
}
/**
* Set to null if empty
* @param $input
*/
public function setLocationIdAttribute($input)
{
$this->attributes['location_id'] = $input ? $input : null;
}
/**
* Set to null if empty
* @param $input
*/
public function setAssignedUserIdAttribute($input)
{
$this->attributes['assigned_user_id'] = $input ? $input : null;
}
/**
* Set attribute to date format
* @param $input
*/
public function setPurchaseDateAttribute($input)
{
if ($input != null && $input != '') {
$this->attributes['purchase_date'] = Carbon::createFromFormat(config('app.date_format'), $input)->format('Y-m-d');
} else {
$this->attributes['purchase_date'] = null;
}
}
/**
* Get attribute from date format
* @param $input
*
* @return string
*/
public function getPurchaseDateAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d', $input)->format(config('app.date_format'));
} else {
return '';
}
}
/**
* Set to null if empty
* @param $input
*/
public function setVendorIdAttribute($input)
{
$this->attributes['vendor_id'] = $input ? $input : null;
}
/**
* Set attribute to money format
* @param $input
*/
public function setPurchasePriceAttribute($input)
{
$this->attributes['purchase_price'] = $input ? $input : null;
}
/**
* Set attribute to date format
* @param $input
*/
public function setWarrantyAttribute($input)
{
if ($input != null && $input != '') {
$this->attributes['warranty'] = Carbon::createFromFormat(config('app.date_format'), $input)->format('Y-m-d');
} else {
$this->attributes['warranty'] = null;
}
}
/**
* Get attribute from date format
* @param $input
*
* @return string
*/
public function getWarrantyAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d', $input)->format(config('app.date_format'));
} else {
return '';
}
}
/**
* Set attribute to date format
* @param $input
*/
public function setAssignedDateAttribute($input)
{
if ($input != null && $input != '') {
$this->attributes['assigned_date'] = Carbon::createFromFormat(config('app.date_format'), $input)->format('Y-m-d');
} else {
$this->attributes['assigned_date'] = null;
}
}
/**
* Get attribute from date format
* @param $input
*
* @return string
*/
public function getAssignedDateAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d', $input)->format(config('app.date_format'));
} else {
return '';
}
}
public function category()
{
return $this->belongsTo(AssetsCategory::class, 'category_id');
}
public function status()
{
return $this->belongsTo(AssetsStatus::class, 'status_id');
}
public function location()
{
return $this->belongsTo(AssetsLocation::class, 'location_id');
}
public function assigned_user()
{
return $this->belongsTo(User::class, 'assigned_user_id');
}
public function vendor()
{
return $this->belongsTo(ContactCompany::class, 'vendor_id');
}
}
但是当我运行它时,出现以下错误 “在字符串上调用between()的成员函数。任何知道为什么我都能得到这个的人
谢谢
答案 0 :(得分:0)
class Data {
final int i;
final String d;
Data({this.i, this.d});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
i: json['i'],
d: json['d'],
);
}
@override
String toString() => return 'i = $i, $d';
}
字段是一个日期,但是laravel不知道它是什么,因此将其视为字符串。为了将其自动转换为Carbon实例,您必须在资产模型中指定它。
将其放在模型的开头(purchase_between
)。
Asset.php
答案 1 :(得分:0)
结果证明该解决方案非常简单,我只是使用Carbon :: parse
所以我的解决办法是
// 2016年
$from2016 = Carbon::parse('2016-04-06');
$to2016 = Carbon::parse('2017-04-05');
$time= Carbon::parse($asset->purchase_date);
if ($time->between( $from2016,$to2016) )
{ ..................