Tengo dos tablas
La tabla activities
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| duration_act | int(11) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
y la tabla activitycategoryareas
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| id_activity | int(10) unsigned | YES | MUL | NULL | |
| id_category_areas | int(10) unsigned | YES | MUL | NULL | |
| ans_act | int(11) | YES | | NULL | |
| duration_act | int(11) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-------------------+------------------+------+-----+---------+----------------+
lo que quiero saber es, cuantas veces se utiliza una actividad en la tabla activitycategoryareas
para ellos hice lo siguiente
public function render()
{
$activities = Activity::all();
$activities = $activities->each( function ($activity) {
$activity->count_activity = Activitycategoryarea::where('id_activity', $activity->id)->count();
return view('livewire.activity.live-activity-table', compact('activities'));
}
y en la vista
<table class="table table-striped table-hover card-table table-vcenter text-nowrap">
<thead>
<tr>
<th>
<div class="mt-3 h6">
{{ __('id Activity') }}
</div>
</th>
<th >
<div class="mt-3 h6">
{{ __('Name Activity') }}
</div>
</th>
<th >
<div class="mt-3 h6">
{{ __('Uso') }}
</div>
</th>
<th scope="col" class="relative px-6 py-3">
<span class="sr-only">Edit</span>
</th>
</tr>
</thead>
@if ($activities->count())
<tbody>
@foreach ($activities as $item)
<tr>
<td>
<div>
{{ $item->id }}
</div>
</td>
<td>
<div style="width: 350px; overflow: hidden; white-space: initial;">
{{ $item->name }}
</div>
</td>
<td>
<div style="width: 350px; overflow: hidden; white-space: initial;">
{{ $item->count_activity }}
</div>
</td>
</tr>
@endforeach
<!-- More people... -->
</tbody>
@else
@livewire('component.live-search-footer')
@endif
</table>
Del atributo count_activity estoy obteniendo la cantidad para mostrarlo en la columna USO
hasta hay todo va bien, el problema viene que intentar aplicar el buscador, paginación y orderBy
al cambiar en controlador en el render por esto
public function render()
{
$activities = Activity::all();
$activities = $activities->each( function ($activity) {
$activity->count_activity = Activitycategoryarea::where('id_activity', $activity->id)->count();
});
return view('livewire.activity.live-activity-table',[
'activities' => $activities
->where('name', 'like', "%{$this->search}%")
->orderBy($this->sort, $this->direction)
->paginate($this->perPage),
]);
}
me da el error
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
No se como organizar el código para que me funcione