Search code examples
phplaravellaravel-8laravel-9

cascaded isset with different if's don't work


How do you set up multiple issets in cascade in a view?

<div class="row">
    <div class="col-12 col-md-10 d-flex justify-content-center">
        @if (@isset($client_name))
        <h1>List of the overall tasks of the{{$client_name}}</h1> 
        @if (@isset($project_name_tasks))
        <h1>List of project tasks of {{$project_name_tasks}} </h1>
        @else
        <h1>List of project tasks</h1>
        @endif
        @endisset
    </div>
</div>

It does not work properly.

Can someone help me?


Solution

  • You should use:

    @isset($client_name)
        <h1>List of the overall tasks of the{{$client_name}}</h1> 
        @isset($project_name_tasks)
            <h1>List of project tasks of {{$project_name_tasks}} </h1>
        @else
            <h1>List of project tasks</h1>
        @endisset
    @endisset
    

    because@isset is Blade directive, so no need to have extra @if.

    Alternatively you can of course use isset function from PHP but then you don't need to prefix it with @ so it should look like:

    @if(isset($client_name))
        <h1>List of the overall tasks of the{{$client_name}}</h1> 
        @if(isset($project_name_tasks))
            <h1>List of project tasks of {{$project_name_tasks}} </h1>
        @else
            <h1>List of project tasks</h1>
        @endif
    @endif
    

    --EDIT--

    If each isset is separate from each other you should just close condition earlier so:

    @isset($client_name)
        <h1>List of the overall tasks of the{{$client_name}}</h1> 
    @endisset
    @isset($project_name_tasks)
       <h1>List of project tasks of {{$project_name_tasks}} </h1>
    @else
      <h1>List of project tasks</h1>
    @endisset
    

    @endisset