0

I can't seem to find an answer simple enough or similar enough. Maybe it's my search terms?

I want to create an array for a form and assign an id to each entry. That way I don't have to call to the db or store a string that might change in the future. The array/object would only have 3-4 values.

For example:

id: 1 name: 'cancel order'

id: 2 name: 'place hold;

The contents would be used in a foreach loop in a laravel blade.

Am I heading in the right direction, or is there a better way to do this?

2 Answers 2

1

Nevermind, I figured that last part out.

Between your answer and mine, I'm good. Thanks!

            <option
               value="{{$changeStatus['id']}}"
                      {{ $changeStatus['name'] == old('changeStatus') ? 'selected' : ''}}>
                      {{ $changeStatus['name'] }}
                       </option>
Sign up to request clarification or add additional context in comments.

1 Comment

You'd want this line to use ['id'], not ['name']: {{ $changeStatus['id'] == old('changeStatus') ? 'selected' : ''}}; the value=... will be in old(), not the name.
1

You mean this?

  $array = [
        "1" => "cancel order",
        "2" => "place hold",
        "3" => "any text"
    ];

Please see: https://www.php.net/manual/de/language.types.array.php

3 Comments

How do I reference the id?
Sorry, hit return too soon. @foreach($changeOrderStatus as $changeStatus) <option value="{{$changeStatus->name}}" {{ $changeStatus->name == old('changeStatus') ? 'selected' : ''}}> {{ $changeStatus->name }} </option> @endforeach How do I format this? Argh!
For reference, with the posted code, it would be @foreach($array as $id => $name)<option value="{{ $id }} {{ old('changeStatus') == $id ? 'selected' : '' }}>{{ $name }}</option> Difference between an array of nested arrays (or objects) and a single associative array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.