Laravel:我不能从控制器向视图发送超过2个变量 [英] Laravel: i can't send more then 2 variables from controller to a view

查看:48
本文介绍了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:

未定义变量:类型(视图:)

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个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆