在字符串上调用()之间的成员函数

问题描述 投票:-1回答:2

我正在建立一个资产管理系统,我试图检查购买日期是否在某个财政年度之间,所以我可以更新资产表中的年份列我在我的控制器中有这个代码:

 //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;使用碳\碳;

/**
* 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');
}

}

但是当我运行它时,我得到以下错误“在字符串之间调用()之间的成员函数。任何人都知道为什么我可以得到这个

谢谢

laravel laravel-5
2个回答
0
投票

purchase_between字段是一个日期,但是laravel不知道它所以它将它视为一个字符串。为了自动将其转换为Carbon实例,您必须在资产模型中指定它。把它放在模型的开头(Asset.php)。

class Asset extends Model{


protected $dates=['purchase_date'];
.
.
.

}

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) )
    { ..................
© www.soinside.com 2019 - 2024. All rights reserved.