Laravel: i can#39;t send more then 2 variables from controller to a view(Laravel:我不能将超过 2 个变量从控制器发送到视图)
                            本文介绍了Laravel:我不能将超过 2 个变量从控制器发送到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
所以我试图从控制器向视图发送一些查询,但是当尝试使用第三个变量时,它说:
So i am trying to send some query from the controller to a view but when try use the third variable it says:
未定义变量:type(View:)
Undefined variable: type(View:)
我在控制器中使用的代码是:
The code i'm using is this in the controller :
    $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
推荐答案
你应该返回一个数组:
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);
这篇关于Laravel:我不能将超过 2 个变量从控制器发送到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:Laravel:我不能将超过 2 个变量从控制器发送到视图
 
				
         
 
            
        基础教程推荐
             猜你喜欢
        
	     - php中的PDF导出 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的foreach复选框POST 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				