Monday, April 10, 2017

Multiple values in laravel post form (dropdown)

Good day fellow programmers,

I have been frustrated all day because I have to make a submit form for an hour register app I have to create for the company I'm an intern at.

So basically what has to be done: I have made a submit form with multiple values, I got asked to make a dropdown selection in which I can put the values of "company name" and "task name". (tasks are connected to the company name). So basically what i'm wondering is how I can put 2 values in one dropdown menu. Having the layout of "$company name - $task name"

create.blade.php

     {!! Form::open(['url' => 'hoursregistrations/create','files'=>true]) !!}

            <div class="form-group" hidden>
                {!! Form::label('user_id', 'User ID: ') !!}
                {!! Form::text('user_id', $authenticated, null, ['class' => 'form-control']) !!}
            </div>

            <select name="">
            @foreach($items as $item)
                <option value="">
                     - 
                </option>
                @endforeach
            </select>

            <div class="form-group">
                {!! Form::label('note', 'Notitie: ') !!} 
                {!! Form::text('note', null, ['class' => 'form-control']) !!}
            </div>

            <div class="form-group">
                {!! Form::label('date', 'Datum: ') !!}
                {!! Form::date('date', null, ['class' => 'form-control']) !!}
            </div>

            <div class="form-group">
                {!! Form::label('hours', 'Uren: (uren-minuten) ') !!}
                {!! Form::time('hours', null, ['class' => 'form-control']) !!}
            </div>



            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-4">
                    <a class="btn btn-danger" href="">
                        @lang('button.cancel')
                    </a>
                    <button type="submit" class="btn btn-success">
                        @lang('button.save')
                    </button>
                </div>
            </div>

            {!! Form::close() !!}

In this code snippet the company variable is $project_id and the task id is $subproject_id.

Also the controller function to redirect the data to the create view

    /**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    $authenticated = Sentinel::getUser()->id;
    $activity_name = Subproject::pluck('id');
    $items = array(
            'company_name' => Company::where('status','client')->pluck('company_name', 'id'),
            'subproject_id' => Subproject::pluck('title', 'id')
        );
        //dd($items);

    return view('hoursregistrations.create', compact('subproject_id', 'authenticated', 
        'company_name', 'activity_name', 'items'));
}

Conclusion: How can I combine the $project_id and $subproject_id into ONE dropdown item.

p.s I'm sorry if it sounds vague I am bad at explaining things



via Kayelder

Advertisement