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

Bono's avatar
Level 1

Undefined variable $plans

Hi! i find this error in my file .blade.php. Can Anybody help?

plans.blade.php (the error is on foreach ($plans as plan)) @extends('layouts.app')

@section('content')

<div class="row text-center align-items-end">
    <div class="col-lg-4 mb-5 mb-lg-0">
        <div class="bg-white p-5 rounded-lg shadow">
        <h1 class="h6 text-uppercase font-weight-bold mb-4">FREE</h1>
        <h2 class="h1 font-weight-bold">€0<span class="text-small font-weight-normal ml-2">/ free</span></h2>

        <div class="custom-separator my-4 mx-auto bg-primary"></div>

        <ul class="list-unstyled my-5 text-small text-left">
            <li class="mb-3">
            <i class="fa fa-check mr-2 text-primary"></i> Consulto gratuito</li>
            <li class="mb-3">
            <i class="fa fa-check mr-2 text-primary"></i> su nuovi pacchetti da realizzare</li>
            <li class="mb-3">
            <i class="fa fa-check mr-2 text-primary"></i> per personalizzare la tua esperienza</li>
            <li class="mb-3 text-muted">
            <i class="fa fa-times mr-2"></i>
            <del>singola o di gruppo</del>
        </ul>
        <a href="#" class="btn btn-primary btn-block shadow rounded-pill">Contattaci ora</a>
        </div>
    </div>

    @foreach($plans as $plan)
    <div class="col-lg-4 mb-5 mb-lg-0">
        <div class="bg-white p-5 rounded-lg shadow">
        <h1 class="h6 text-uppercase font-weight-bold mb-4">{{ $plan->nome }}</h1>
        <h2 class="h1 font-weight-bold">€{{ $plan->prezzo }}<span class="text-small font-weight-normal ml-2">{{ $plan->fatturazione}}</span></h2>

        <div class="custom-separator my-4 mx-auto bg-primary"></div>

        <ul class="list-unstyled my-5 text-small text-left font-weight-normal">
            <li class="mb-3">
            <i class="fa fa-check mr-2 text-primary"></i>{{$plan->difficoltà}}</li>
            <i class="fa fa-check mr-2 text-primary"></i>{{$plan->descrizione}}</li>
            <i class="fa fa-check mr-2 text-primary"></i>{{$plan->esperienza}}</li>
            <i class="fa fa-check mr-2 text-primary"></i>{{$plan->durata}}</li>
            <i class="fa fa-check mr-2 text-primary"></i>{{$plan->salita}}</li>
            <i class="fa fa-check mr-2 text-primary"></i>{{$plan->discesa}}</li>
            </li>
        </ul>
        <a href="{{ route('plans.show', $plan->tipo_pacchetto) }}" class="btn btn-primary btn-block shadow rounded-pill">Compra ora</a>
        </div>
    </div>
    @endforeach
</div>

namespace App\Http\Controllers;

use App\Models\Plan; use Exception; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Models\User;

class SubscriptionController extends Controller {

//da web.php
public function showPlanForm()
{
    return view('stripe.plans.create'); 
    /*
    plans products that where user can subscribe against a product
    and a subscription is activated

    it's based on billing method id user set it to weekly so it will charge
    the user weekly
    */
}
//da web.php
public function savePlan(Request $request)
{
    /* Prima sol return $request->all(); */
    //inserisco la chiave api
    \Stripe\Stripe::setApiKey(config('services.stripe.secret')); //deve restituire success 
    //amount, concetto stdrd
    $amount = ($request->amount * 100); 
    try{
        $plan = Plan::create([
            'amount' => $amount,
            'currency' => $request->currency,
            'interval' => $request->billing_period, //way to specify some billing period
            // puoi modificare il parametro con 'week' 'year'
            //definizione propria di interval_count
            /*
            'interval => 'week', cambio con $request->billing_period
            'interval_count' => $request->interval_count
             */
            'interval_count' => $request->interval_count,
            'product' =>[
                'name' => $request->name
            ]
            /*
            'name' => $request->name 
            it's actually product and here we need to specify an array, 
            with a name attribute. getting from a form
            */
        ]);
        //dd($plan); low-level data copying and conversion tasks
        
        //laravel plan ModelsPlan
        Plan::create([
            'plan_id' => $plan->id,
            'name' => $request->name, //non product e al posto di plan usa $request
            'price' => $plan->amount,
            'billing_method' => $plan->interval,
            'currency' => $plan->currency,
            'interval_count' => $plan->interval_count
        ]);// extepct an array, you to define 
        
    }catch(Exception $ex){// exception is a php native class
        dd($ex->getMessage());// catch the exception type
    }
    //valore di ritorno messaggio di successo
    return "success";
}

public function allPlans()
{ 
    // Fetch the plans from Stripe
    $plans = Plan::get();

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

public function show(Plan $plan, Request $request)
{
    $intent = auth()->user()->createSetupIntent();

    return view("subscription", compact("plan", "intent"));
}

public function subscription(Request $request)
{
    $plan = Plan::find($request->plan);
    $subscription = $request->user()->newSubscription($request->plan, $plan->codice_stripe)
    ->create($request->token);

    return view("subscription_success");
}
   

} My route in web.php Route::middleware("auth")->group(function () { Route::get('plans', [SubscriptionController::class, 'allPlans']); Route::get('plans/{plan}', [SubscriptionController::class, 'show'])->name("plans.show"); Route::post('subscription', [SubscriptionController::class, 'subscription'])->name("subscription.create"); });

0 likes
5 replies
LaryAI's avatar
Level 58

The error "Undefined variable $plans" occurs because the variable $plans is not defined in the plans.blade.php file. To fix this error, you need to pass the $plans variable from the controller to the view.

In the SubscriptionController class, modify the allPlans method as follows:

public function allPlans()
{ 
    // Fetch the plans from Stripe
    $plans = Plan::get();

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

Then, in your web.php file, make sure to import the SubscriptionController class at the top:

use App\Http\Controllers\SubscriptionController;

Finally, update your routes to use the SubscriptionController:

Route::middleware("auth")->group(function () {
    Route::get('plans', [SubscriptionController::class, 'allPlans']);
    Route::get('plans/{plan}', [SubscriptionController::class, 'show'])->name("plans.show");
    Route::post('subscription', [SubscriptionController::class, 'subscription'])->name("subscription.create");
});

Now, when you visit the /plans route, the $plans variable will be passed to the plans.blade.php view, and the error should be resolved.

tykus's avatar

You probably are returning the plans.blade.php view template from another Controller action / Route Closure - it is difficult to read your unformatted code...

Snapey's avatar

share the full error? There is a share link on the error page

Bono's avatar
Level 1

[2023-06-21 17:24:23] local.ERROR: Undefined variable $biking {"view":{"view":"/home/bono/Desktop/progetto_tecweb/progetto_tecweb/travelApp/resources/views/stripe/plans.blade.php","data":[]},"userId":1,"exception":"[object] (Spatie\LaravelIgnition\Exceptions\ViewException(code: 0): Undefined variable $biking at /home/bono/Desktop/progetto_tecweb/progetto_tecweb/travelApp/resources/views/stripe/plans.blade.php:50) [stacktrace] it coud you help?

Snapey's avatar

@Bono no thats not what I meant, and besides

Undefined variable $biking

is this a second question?

Please or to participate in this conversation.