You can use the select method on the whereRelation method to select only the needed column. Here's an example:
Task::query()
->whereRelation(
'user', function ($query) use ($request) {
$query->select('name')->where('name', 'like', "%{$request->search}%");
}
)
->get();
In this example, we're passing a closure to the whereRelation method, which allows us to use the select method to select only the name column from the user relationship. We're also using the where method to filter the results based on the search term.
Note that we're using the use keyword to pass the $request variable to the closure. This is necessary because closures have their own scope and don't inherit variables from the parent scope.