Home > Enterprise >  Undefined variable $payments laravel 9.x
Undefined variable $payments laravel 9.x

Time:07-16

I am trying to print out the lists of payment methods using the foreach loop, however, i get an undefined variable.. error.

Here is my controller class

    class HomeController extends Controller
{
    //
    public function index()
    {
        $payments = Payment::where('status', 1)->get();

        return view('user.account.index', [
            'payments' => $payments
        ]);
    }
}

and here is my section in my view page(index) i want to loop through

@foreach ($payments as $payment)
                  <!-- {{ Cryptocap::getSingleAsset($payment->name) }} -->
                  <div >
                      <div >
                          <div >
                              <i ></i>
                          </div>

                          <div >
                              <span >0.0012930403</span>
                          </div>

                          <small >BTC</small>
                          <div  style="width: 40%; height: 5px;"></div>
                      </div>
                  </div>
        @endforeach

The above approach works on other areas of my application, as this is the approach i have been using throughout the application. But it doesn't seem to work on this particular controller and view.

CodePudding user response:

in your controller do this

class HomeController extends Controller
{
    //
    public function index()
    {
        $payments = Payment::where('status', 1)->get();

        return view('user.account.index',compact('payments'));
    }
}

or

class HomeController extends Controller
    {
        //
        public function index()
        {
            $payments = Payment::where('status', 1)->get();
    
            return view('user.account.index')->with('payments',$payments);
        }
    } 

CodePudding user response:

Can you give this a try

    return View::make("user/regprofile", compact('students')); OR
    return View::make("user/regprofile")->with(array('students'=>$students));

For more info visit this link Passing data from controller to view in Laravel

  • Related