To retrieve the array value from database to my view blade

Published 1 week ago by Niyaz

array:13 [▼ "_token" => "K5eGLUL5zZDaemAKZljf2FuA44Qxb1oA8yqnxbWX" "community_id" => "1" "name" => "nias" "building_no" => "54" "unit_no" => "" "mobile" => "984" "email" => "" "request_status" => array:4 [▼ 0 => "1" 1 => "2" 2 => "3" 3 => "4" ] "vehicle_one" => "" "registration_one" => "" "vehicle_two" => "" "registration_two" => "" "agree" => "agree" ]

Here is my input from my form and I need to get the values of the array in the name "request_status" from the database to my view blade how to get it? anyone knows about it

Sergiu17
Sergiu17
1 week ago (140,580 XP)
dd($request->request_status); // here's the array.

@foreach( $request->request_status as $status)
    {{ $staus }} // prints '1234' in your ca
@endforeach
Cronix
Cronix
1 week ago (482,560 XP)

I need to get the values of the array in the name "request_status" from the database

If I'm understanding correctly, you want to retrieve the array from your form, called 'request_status', and then use the values from that array to search a column in the db called 'request_status'?

$requestStatus = $request->request_status; // the array from the form

// get the records where request_status column is values from array
$record = YourModel::whereIn('request_status', $requestStatus)->get();
Niyaz

Thank you for the replies since i'm new to laravel .And i have an another query like can i have if condition to print different item

Here is my code

Reason For Request @foreach( $request->request_status as $status) @if ($status == 1 && $status == 2 && $status == 3 && $status == 4 && $status == 5) Re-activation, Additional Access, Lost, Damaged, Replacement @elseif ($status == 1 && $status == 2 && $status == 3 && $status == 4) Re-activation, Additional access, Lost, Damaged @elseif ($status == 1 && $status == 2 && $status == 3) Re-activation, Additional access, Lost @elseif ($status == 1 && $status == 2) Re-activation, Additional access @elseif ($status == 1) Re-activation @else None of them is selected @endif @endforeach Please re-correct and provide me a better solution Thank you
mvd
mvd
4 days ago (23,250 XP)

Hi @Niyaz ,

Try to use the code tags, it's hard to read your code. https://help.github.com/articles/basic-writing-and-formatting-syntax/#quoting-code

Use the switch statement

@foreach( $request->request_status as $status)

    @if (!$status)
        None of them is selected
    @else    

        @switch ($status)
            @case(5)
                Re-activation, Additional Access, Lost, Damaged, Replacement
                @break
            @case(4)
                Re-activation, Additional access, Lost, Damaged
                @break
            @case(3)
                Re-activation, Additional access, Lost
                @break
            @case(2)
                Re-activation, Additional access
            @default
                Re-activation

        @endswitch
        
    @endif    
@endforeach 

Or (cleaner method is pass the $statusInfo from your controller)

    {{
        $statusInfo = array(
            1 => 'Re-activation',
            2 => 'Re-activation, Additional access',
            3 => 'Re-activation, Additional access, Lost',
            4 => 'Re-activation, Additional access, Lost, Damaged',
            5 => 'Re-activation, Additional Access, Lost, Damaged, Replacement'
         );
    }}

    @if (isset($statusInfo[$status]))
        {{ $statusInfo[$status] }}
    @else
        None of them is selected
    @endif
Niyaz

Hi @mvd ,

I tried your code of using switch in my blade And got the output as :

 Reason For Request :
 @switch ($status) @case(5) Re-activation, Additional Access, Lost, Damaged, Replacement

Instead of only displaying Reactivation,Additional Access, Lost, Damaged, Replacement

mvd
mvd
4 days ago (23,250 XP)

@Niyaz did you set each switch, case, breaks etc on a new line just like my code?

Niyaz

Hi

I tried using the controller

$statusInfo = array(
            1 => 'Re-activation',
            2 => 'Re-activation, Additional access',
            3 => 'Re-activation, Additional access, Lost',
            4 => 'Re-activation, Additional access, Lost, Damaged',
            5 => 'Re-activation, Additional Access, Lost, Damaged, Replacement'
         );

and on my blade


@foreach( $request->request_status as $status)
               @if (isset($statusInfo[$status]))
               {{ $statusInfo[$status] }}
             @else
               None of them is selected
            @endif
            @endforeach 

But my output is

Reason For Request:
 None of them is selected None of them is selected None of them is selected

It displays 3 times "None of them selected" instead of values

And this is my database table and their column is

  id            Name                      request_status

  1              niyaz                        ["1","2","3"]

Have to retrieve the values of "request_status"(column) into my blade template in the name as mention above.

And last but not the least , those values("1,2,3") are my checkbox value

Niyaz

Yes @mvd ,

i did as per your code used switch, case, breaks in every line

mvd
mvd
4 days ago (23,250 XP)

@Niyaz did you pass the $statusInfo array from your controller to your view? Something like

return view('view_name', ['statusInfo' => $statusInfo ....

i did as per your code used switch, case, breaks in every line

Which version of Laravel do you use?

Niyaz

@mvd

Thank you very much for your help All my requriements is working

mvd
mvd
4 days ago (23,250 XP)

Nice!

Please sign in or create an account to participate in this conversation.