2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2021-01-26 12:44:12 +06:00
|
|
|
use Illuminate\Support\Str;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
trait Relationships
|
|
|
|
{
|
|
|
|
public function countRelationships($model, $relationships)
|
|
|
|
{
|
|
|
|
$counter = [];
|
|
|
|
|
|
|
|
foreach ($relationships as $relationship => $text) {
|
|
|
|
if (!$c = $model->$relationship()->count()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-26 12:44:12 +06:00
|
|
|
$text = Str::contains($text, '::') ? $text : 'general.' . $text;
|
|
|
|
$counter[] = $c . ' ' . strtolower(trans_choice($text, ($c > 1) ? 2 : 1));
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mass delete relationships with events being fired.
|
|
|
|
*
|
|
|
|
* @param $model
|
|
|
|
* @param $relationships
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function deleteRelationships($model, $relationships)
|
|
|
|
{
|
|
|
|
foreach ((array) $relationships as $relationship) {
|
|
|
|
if (empty($model->$relationship)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = $model->$relationship->all();
|
|
|
|
|
|
|
|
if ($items instanceof Collection) {
|
|
|
|
$items = $items->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ((array) $items as $item) {
|
|
|
|
$item->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|