I have the following function in the Controller:
public function index($id)
{
$client_name_tasks = Client::select('clients.name')
->join('projects', 'clients.id', '=', 'projects.client_id')
->join('tasks', 'projects.id', '=', 'tasks.project_id')
->where('tasks.project_id', $id)
->distinct()
->get();
return view('task.index', compact('client_name_tasks'));
}
where $client_name_tasks is collection.
Since the following view is called from different parts of my project, the title h1 is different based on where it is called (are 3 different).
<div >
@isset($client_name)
<h1>List of overall tasks of {{$client_name}}</h1>
@endisset
@isset($client_name_tasks)
@foreach($client_name_tasks as $client_name_task)
<h1>List of {{$client_name_task->name}} project tasks</h1>
@endforeach
@else
<h1>List all task</h1>
@endisset
</div>
I get error compact(): Error $client_name_tasks.
The problem is in the view, isset.
Can anyone kindly help me?
CodePudding user response:
You can check the count value of that collection.
<div >
@isset($client_name)
<h1>List of overall tasks of {{$client_name}}</h1>
@endisset
@if(count($client_name_tasks) > 0)
@foreach($client_name_tasks as $client_name_task)
<h1>List of {{$client_name_task->name}} project tasks</h1>
@endforeach
@else
<h1>List all task</h1>
@endif
</div>
CodePudding user response:
why don't you do a foreach in your controller in order to bring putting it in the compact back only the variable in the isset?
foreach ($client_name_tasks as $client_name_task){
$name_client=$client_name_task->name;
}
and in the view write this way (attention that name_client is different from client_name):
@if(isset($name_client))
<h1>List of {{$client_name_task->name}} project tasks</h1>
@elseif(isset($client_name))
<h1>List of overall tasks of {{$client_name}}</h1>
@else
<h1>All task</h1>
@endif