所以我试图从控制器向视图发送一些查询,但是当尝试使用第三个变量时,它说:
未定义的变量:type(View :)
我使用的代码是控制器中的代码:
$doc=DB::table('documents')
->join('users', 'users.id', '=', 'documents.id_user')
->join('type_docs', 'type_docs.id', '=', 'documents.id_tipo_doc')
->join('departments', 'departments.id', '=', 'documents.id_departamento')
->select('documents.*', 'type_docs.type', 'users.name','departments.abbreviation')
->get();
$user=DB::table('users')
->select('users.*')
->get();
$type=DB::table('type_docs')
->select('type_docs.*')
->get();
//$doc = Document::all();
return view('dashboard',['doc'=>$doc],['user'=>$user],['type'=>$type]);
并在视图中:
@foreach($type as $types)
<option value="{{$types->id}}">{{$types->type}}</option>
@endforeach
答案 0 :(得分:10)
你应该返回一个数组:
return view('dashboard',['doc'=>$doc,'user'=>$user,'type'=>$type]);
我们还有其他方式:
return view('dashboard', array('doc'=>$doc,'user'=>$user,'type'=>$type));
return view('dashboard', compact('doc','user','type'));
return view('dashboard')
->with('doc', $doc)
->with('user', $user)
->with('type', $type);
return view('dashboard') //using laravel Magic method.
->withDoc($doc)
->withUser($user)
->withType($type);