错误传递变量进行查看

时间:2017-01-14 10:51:30

标签: php laravel laravel-5.3 backend

我在将数据从控制器传递到刀片时遇到此错误,每件事情对我来说都很好但我不知道我错过了什么

ErrorException in 3f66fc6a06d60b9d9b9d521f50ebd1074b04f3bc.php line 228:
Undefined variable: users (View: C:\wamp\www\transport_mithra\resources\views\agents.blade.php)

我的控制器是

<?php

namespace App\Http\Controllers;

use Response;
use Session;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class UsersController extends ApiController
{
    public function index() 
    {
        $users = User::all();

        return View('agents', compact($users));

        //return view('agents')->with('users', $users);  with this is also same error
    }   
}

和我的agents.blade.php

@foreach ($users as $agent)

<div class="col s12 m6 l3">
    <div class="card small card-agent">
        <div class="card-image">
            <img src="assets/images/profile-image-2.png" alt="">
        </div>
        <div class="card-content">
            <div class="card-details">
                <p>Name:</p><span>{{ $agent->name }}</span>
            </div>
            <div class="card-details">
                <p>Phone:</p><span>{{ $agent->phone_number }}</span>
            </div>
            <div class="card-details">
                <p>Email:</p><span>{{ $agent->email }}</span>
            </div>
            <div class="card-details">
                <p>Address:</p><span>{{ $agent->address_city_village }}</span>
            </div>
        </div>
        <div class="card-action">
            <p>Drivers: 502</p>
            <a href="drivers" class="waves-effect waves-light btn green">View</a>
        </div>
    </div>
</div>

@endforeach

期待急需的帮助

谢谢

2 个答案:

答案 0 :(得分:2)

您应该使用正确的语法:

return view('agents', compact('users'));

compact('users')['users' => $users]

相同

答案 1 :(得分:2)

以下内容也适用:

return view('agents')->with(['users' => $users]);