Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bono's avatar
Level 1

Would like to display to screen my variable on file blade.php

Hello, I am using Laravel 10 for a university project. i would fetch my Plan model (using his controller called subscriptioncontroller.php) from stripe and show t(hree variables created in the controller in the method "allPlans" which represent 3 type of travel pkts i would use for my application) to user (through my plans.blade.php). Earlier when i open on http://127.0.0.1:8000 and search "/plans" in the seach bar i encountered error "Undefined variable $biking" highlighted on my istruction "@if($biking)". I show my files below:

SubscriptionController.php (i use both Plan and ModelsPlan)

0 likes
6 replies
LaryAI's avatar
Level 58

To display a variable on a Blade template in Laravel, you need to pass the variable from the controller to the view. In this case, you want to display the three variables created in the allPlans method of the SubscriptionController on the plans.blade.php file.

To do this, you can pass the variables as an array to the view function in the controller method. Here's an example of how you can achieve this:

// SubscriptionController.php

use App\Models\Plan;

class SubscriptionController extends Controller
{
    public function allPlans()
    {
        // Fetch the plans from Stripe
        $plans = Plan::all();

        // Create the three variables representing the travel packets
        $biking = $plans->where('type', 'biking')->first();
        $hiking = $plans->where('type', 'hiking')->first();
        $swimming = $plans->where('type', 'swimming')->first();

        // Pass the variables to the view
        return view('plans', compact('biking', 'hiking', 'swimming'));
    }
}

In the above code, we fetch the plans from Stripe and create the three variables representing the travel packets. Then, we pass these variables to the plans.blade.php view using the compact function.

Now, in your plans.blade.php file, you can access these variables using the {{ }} syntax. Here's an example:

<!-- plans.blade.php -->

@if ($biking)
    <p>Biking Plan: {{ $biking->name }}</p>
@endif

@if ($hiking)
    <p>Hiking Plan: {{ $hiking->name }}</p>
@endif

@if ($swimming)
    <p>Swimming Plan: {{ $swimming->name }}</p>
@endif

In the above code, we use the @if directive to check if each variable exists before displaying its value. If the variable exists, we display its name using the {{ }} syntax.

Make sure to replace Plan with the actual model class you are using for your plans.

This should solve the issue of the "Undefined variable $biking" error and display the variables on the plans.blade.php file.

Bono's avatar
Level 1

how can i verify if the plans are fetchble from stripe?

1 like
Bono's avatar
Level 1

@tisuchi sorry and do you think i would look else for resolve my problem because i do like "LaryAI" said but it doesn't change my situation

1 like
tisuchi's avatar

@Bono No.

There is no point to paste the same content again, that's what I meant.

1 like

Please or to participate in this conversation.