Marcel Santing
Marcel Santing

Reputation: 103

How can I return properly an array value in blade laravel view

I am using a Database tables component for Laravel, however I want to do a check within the blade view, but can't seem to figure out the best approach for that. Since the array is created in the view and not in the controller.

I have an array and an object value. And if that object value is true it should present an extra line within the array.

What I have is the following: "title" => "view_template"

What I want to produce is the following

[
    'sometitle1' => 'someview1',
    'sometitle2' => 'someview2', //this part needs if statement is true
    'sometitle3' => 'someview3'
]

I am thinking of something like this

[
    'sometitle1' => 'someview1',
    ($obj->has_this_field) ? ['sometitle2' => 'someview2']:
    'sometitle3' => 'someview3'
]

But it doesn't do that obviously. I normally solve this with array_push in the controller. What would be the best approach since this is in a blade view.

I also tried this

[
    'sometitle1' => 'someview1',
    ($obj->has_this_field) ? ['sometitle2' => 'someview2']:
    'sometitle3' => 'someview3'
]

And this will obviously not work

[
    'sometitle1' => 'someview1',
    ($obj->has_this_field) ? 'sometitle2' => 'someview2':
    'sometitle3' => 'someview3'
]



@include('partials.panel', [
    'header' => trans('general.information'),
    'partial' => 'partials.show-tabs',
        'partialData' => [
            'root' => 'equipment.equipment.panels',
            'tabs' => [
                'general' => 'general',
                'advanced' => 'maintenance',
                (!$equipment->has_running_hours) ? ['runninghours' => 'runninghours']:
                'history' => 'history',
            ]
        ]
    ])

This is what I want to produce

[
    'general' => 'general',
    'runninghours' => 'runninghours',
    'history' => 'history'
]

Upvotes: 0

Views: 951

Answers (1)

Karan Sadana
Karan Sadana

Reputation: 1373

Why you don't make an array first with @php //your Logic @endphp then pass that array to your @include part

Something like below.

@php
$array = [
    'header' => trans('general.information'),
    'partial' => 'partials.show-tabs',
        'partialData' => [
            'root' => 'equipment.equipment.panels',
            'tabs' => [
                'general' => 'general',
                'advanced' => 'maintenance',
            ]
        ]
    ];
if(!$equipment->has_running_hours)
{
 $array['partialData']['tabs']['runninghours'] = 'runninghours';
}
else
{
 $array['partialData']['tabs']['history'] = 'history';
}

    @endphp

Now pass this to your @include

@include('partials.panel',$array)

Upvotes: 1

Related Questions