Laravel: i can't send more then 2 variables fr

2019-01-09 19:50发布

问题:

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]);

and in the view:

       @foreach($type as $types)
                  <option value="{{$types->id}}">{{$types->type}}</option>
       @endforeach

回答1:

You should return one array :

return view('dashboard',['doc'=>$doc,'user'=>$user,'type'=>$type]);

There is other ways such us :

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);