Xel
Xel

Reputation: 15

How to display some values fron option php

In database, i have 4 values for pricing plan: - Star (plan_id = 1) - Moon (plan_id = 2) - Sun (plan_id = 3) - Galaxy (plan_id = 4)

On signup page, I only want to display plan_id 2 & 3. Is it possible?

My current code for the pricing plan option as follow:

               <div class="form-group">
                    <label for="plan_id">Plan</label>
                    <select class="form-control select2" name="plan_id" id="plan_id" required>
                       <option value="" disabled selected>Choose a plan</option>
                       @foreach($plan as $plans)
                          <option value="{{ $plans->id }}">{{ $plans->plan_name }}</option>
                       @endforeach
                    </select>
                 </div>

Will appreciate any help you can. Thank you

Upvotes: 0

Views: 16

Answers (1)

pew007
pew007

Reputation: 473

You'll want to keep the data manipulate to a minimum in your template so ideally you'll handle the data in your controller and pass it to the template. Is the code above shared by the signup page and other pages? If not, then you can filter the data in the controller to only return plan_id 2 and 3 and then pass that to the template.

in the controller:

$plans = [
    ['id' => 1, 'plan_name' => 'Star'],
    ['id' => 2, 'plan_name' => 'Moon'],
    ['id' => 3, 'plan_name' => 'Sun'],
    ['id' => 4, 'plan_name' => 'Galaxy'],
];

$filtered = array_filter($plans, function($plan) {
    return in_array($plan, [2, 3]);
});

Then you can pass $filtered to your template.

Upvotes: 1

Related Questions