QR_code_generator/app/Support/ClassifiableTrait.php

28 lines
834 B
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

<?php
namespace App\Support;
use App\Models\Classification\Code;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Str;
trait ClassifiableTrait {
public function codes(): MorphToMany {
return $this->morphToMany(Code::class, 'classifiable', 'classifiable')->withTimestamps();
}
public function getCodes($classifier) {
return $this->codes()->whereHas('classifier', function($query) use($classifier) {
$query->where(['name' => $classifier]);
})->get();
}
public function setCodes($codes) {
$codes = collect($codes)->map(function($code) {
return trim(Str::replace(' ', '', $code));
})->all();
$this->codes()->sync(Code::query()->whereIn('name', $codes)->orWhereIn('uuid', $codes)->pluck('id')->all());
}
}