phplaravellaravel-5

Call to a member function between() on string


i am building an asset management system and i am trying to checkif the purchase date is between a certain financial year so i can update the year column in the assets table i have this code in my controller:

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



    }

    }

This is my Model

use Illuminate\Database\Eloquent\Model; use 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');
}

}

but when i run it, i get the following error "Call to a member function between() on string. Anybody with an idea why i could be getting this

Thanks


Solution

  • Turns out the solution was really simple, i just used Carbon::parse

    So my solution was

    //year 2016

    $from2016 = Carbon::parse('2016-04-06');
    $to2016 = Carbon::parse('2017-04-05');
    
    
    $time= Carbon::parse($asset->purchase_date);
    
     if ($time->between( $from2016,$to2016) )
        { ..................