1

I have created a small applications and I am creating an edit form. I have 2 tables one "cars" and the other named "classes". Table clrs has a field named class which has all id of table classes. I have join query which gets data from 2 tables and displays at the form. The problem is that I want to create a dropdown list at field "class" which gets all classes from table classes and displays with a dropdown, but also I want to set the default value the class which is the id in table "cars".

 public function edit($id) {
        $values = DB::table('cars')
                ->join('classes', 'cars.class', '=', 'classes.id')
                ->select('cars.*', 'classes.class')
                ->where ('cars.id','=',$id)
                ->get();
        return View::make('pages.edit')->with('values', $values);
    }

edit.blade.php

<div class="form-group">
                {{ Form::label('class', 'Class', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::text('class', $v->class, array('class' => 'form-control')) }}
                </div>
            </div>

I want to create an other query at controller that gets all classes existing in table classes and to put them at the form as a dropdown list. I don't know how to pass 2 arrays at view. I really need some help

1
  • Do you mean View::make(...)->with('values1',$values1)->with('values2',$values2) ? Commented Apr 25, 2014 at 14:01

1 Answer 1

2

Just pass an array, it's way easier:

    public function edit($id) {
      $data['values'] = DB::table('cars')
                      ->join('classes', 'cars.class', '=', 'classes.id')
                      ->select('cars.*', 'classes.class')
                      ->where ('cars.id','=',$id)
                     ->get();
      $data['otherArray'] = ['other', 'elements'];
      $data['anotherVar'] = 'This is just a string';

      return View::make('pages.edit', $data);
   }

In your view you'll have $values, $otherArray, $anotherVar and so on.

Sign up to request clarification or add additional context in comments.

5 Comments

@foreach ($values as $v) shows me the error: undefined variable values
It works now. The problem is with foreach now. Because at dropdown I want to show as default value the value of class that I get from $values and as dropdown options all the values from second array
Form::select('class', $second_array, $values->id); The second parameter is your list of options, the third one, your default value.
{{ Form::select('class', $class , $v->class) }} and it doesn't appear. the default value is blank

Your Answer

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