Reputation:
I am trying to store values of 2 different tables in an array and then to display values in view. I have 2 tables "cars" and "classes". Table cars has a field named "class" which contain the id of class table and table class has fields "class" and "id". I am making the follow queries:
public function edit($id) {
$data['values'] = DB::select('select * from cars where id = ?', array($id));
$class = DB::select('select class from cars where id = ?', array($id));
$data['class']= DB::select('select class from classes where id = ?', array($class));
$data['classes'] = DB::table('classes')->orderBy('class', 'asc')->distinct()->lists('class', 'id');
return View::make('pages.edit', $data);
}
and in view:
<div class="form-group">
{{ Form::label('class', 'Class', array('class'=>'control-label col-lg-4')) }}
<div class="col-lg-8">
{{ Form::select('class', $classes ,$class->class,array('class' => 'form-control') ) }}
</div>
</div>
in $class i want to save the id of class for that specific car. Is there any other way to do this without inner join ?? it displays the following error:
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
Upvotes: 0
Views: 2411
Reputation: 81157
// this returns array(stdClass('class'=>'classValue'))
$data['class']= DB::select('select class from classes where id = ?', array($class));
// so simply do this:
$classArray= DB::select('select class from classes where id = ?', array($class));
$data['class']= $classArray[0];
Upvotes: 3