Reputation: 2245
I'm trying to get this custom method in my Model to work:
Model:
public function qrCode()
{
$binCode = $this->code ? $this->code : 123456;
return $this->attributes['qr_code'] = QrCode::size(200)->generate($binCode);
}
Controller:
$AllBins = Bin::where([['id', '>=', $fromBinId], ['id', '<=', $toBinId]])->get();
$qrCodes = $AllBins->qrCode();
It's giving this error Method Illuminate\Database\Eloquent\Collection::qrCode does not exist
I've also tried with('qrCode')
and doesn't work either.
Any help/advice would be appreciated!
Upvotes: 0
Views: 714
Reputation: 12391
try laravel Appending Values To JSON
ref link https://laravel.com/docs/8.x/eloquent-serialization#appending-values-to-json
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['qr_code'];
public function getQrCodeAttribute()
{
$binCode = $this->code ? $this->code : 123456;
return $this->attributes['qr_code'] = QrCode::size(200)->generate($binCode);
}
by this $AllBins
in this collection new key qr_code
will be added
so you can use pluck() to get all qr_code
$qrCodes = $AllBins->pluck('qr_code');
Upvotes: 1