Unknown column inspection_date in order clause, is because the inspection_date column is defined in the histories table, not the posts table. You're trying to order the posts table by a column that it doesn't have, which is why you're getting this error.
To order the posts table by the inspection_date column from the related histories table, you should modify your query to join the histories table and then order by inspection_date.
this code as reference
class PostController extends Controller {
public function index() {
$orderColumn = request('order_column', 'inspection_date');
if (!in_array($orderColumn, ['no', 'inspection_date'])) {
$orderColumn = 'inspection_date';
}
$orderDirection = request('order_direction', 'desc');
if (!in_array($orderDirection, ['asc', 'desc'])) {
$orderDirection = 'desc';
}
$posts = Post::with(['lastHistory'])
->leftJoin('histories', 'posts.id', '=', 'histories.post_id')
->when(request('category'), function($query) {
$query->where('division', request('category'));
})
->orderBy('histories.' . $orderColumn, $orderDirection)
->select('posts.*')
->paginate(10);
return PostResource::collection($posts);
}
}