Home  >  Q&A  >  body text

How to display value from controller to Blade file in php laravel?

From controller:

$response = (new ApiController)->checkNumbers();
    if(!empty($response['status']) && ($response['status'] == 'SUCCESS')) {
                $numbers = json_encode($response['numbers']);
    }
    return [
              'html' => view('show.numbers', compact(
                        'numbers'
                    ))->render()
                ];

How to display "numbers" in the blade file to reflect the UI in the table? Any help would be greatly appreciated.

Thank you so much

P粉842215006P粉842215006335 days ago430

reply all(1)I'll reply

  • P粉138871485

    P粉1388714852023-12-13 10:35:18

    The following example shows how to pass data from the controller and populate it to the blade.

    For example, I have a table: 'user', which contains id, first name and last name columns:

    SampleController.php

    public function FetchUser(){
        $data = User::all();
        
        return view('MyView')->with([
         'data' => $data
        ]);
    }

    MyView.blade.php

    {{-- populate it in a table --}}
    
    @foreach($data as $item){
        
    }
    
    ID First Last
    {{$item->id}} {{$item->firstname}} {{$item->lastname}}

    reply
    0
  • Cancelreply