Level 2
I should've used ->value('name') instead of first('name'). Solved.
Hey, I need to cycle through pagination data and change a value based on a condition, but I'm getting this error: Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given
HouseController:
<?php
namespace App\Http\Controllers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class HouseController extends Controller
{
public function index()
{
if(request('town')) {
$this->validate(
request(),
[
'town' => 'string|in:' . implode(',', config('aac.towns')),
],
[
'*.string' => 'Town can only be a string.',
'*.in' => 'Unknown town.'
]
);
$town = request('town');
}
else $town = config('aac.towns.' . config('aac.houses.defaultHouselistTown'));
$town_id = array_search($town, config('aac.towns'));
if(request('orderBy')) {
$this->validate(
request(),
[
'orderBy' => 'string|in:name,size,beds,rent,owner',
],
[
'orderBy' => 'You can only order by: name, size, beds, rent, owner'
]
);
$orderBy = request('orderBy');
}
else $orderBy = 'name';
if(request('order')) {
$this->validate(
request(),
[
'order' => 'string|in:desc,asc'
],
[
'order.string' => 'You can only use asc or desc order.',
]
);
$order = request('order');
}
else $order = 'desc';
$currentPage = request('page') ? request('page') : 1;
// dd($order, $orderBy, $town, $town_id);
$data = Cache::remember('houses_' . $town . '_' . $orderBy . '_' . $order . '_' . $currentPage, 1, function () use ($town, $orderBy, $order, $town_id, $currentPage) {
$houses = DB::table('houses')
->where('town_id', $town_id)
->orderBy($orderBy, $order)
->select('id', 'name', 'size', 'beds', 'rent', 'owner')
->paginate(15);
foreach($houses as $house) {
if ($house->owner != '0')
$house->owner = DB::table('players')->where('id', $house->owner)->first('name');
}
Paginator::currentPageResolver(function () use ($currentPage) {
return $currentPage;
});
return $houses;
});
return view('houses', [
'houses' => $data,
'town' => $town,
'orderBy' => $orderBy,
'order' => $order
]);
}
}
I should've used ->value('name') instead of first('name'). Solved.
Please or to participate in this conversation.