未定义的变量:Laravel foreach中的错误

时间:2019-06-26 04:57:26

标签: php laravel laravel-5

我正在尝试从数据库中检索数据到我的Laravel CRUD中,但是它在我的视图中显示未定义的变量。

这是view frame.blade.php

@foreach($mahasiswa as $mhs)
   <tr>
       <td>{{$mhs->id}}</td>
       <td>{{$mhs->nim}}</td>
       <td>{{$mhs->nama}}</td>
       <td>{{$mhs->alamat}}</td>
       <td>{{$mhs->fakultas}}</td>
       <td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit">&#xE254;</i></a>
           <a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete">&#xE872;</i></a>
       </td>
       </tr>
@endforeach

这是控制器

use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class DashboardController extends Controller
{

public function index(Request $request)
{
    $mhs = Dashboard::orderBy('id', 'desc')->paginate(5);

    return view('frame')->with('frame', $mhs);
}

这是路线

Route::group(['prefix' => 'mahasiswa'], function () {
    Route::get('/dashboard/{id}', [
        'uses' => 'DashboardController@show',
        'as'   => 'mahasiswa.show',
    ]);

    Route::post('/dashboard/', [
        'uses' => 'DashboardController@store',
        'as'   => 'mahasiswa.store',
    ]);

    Route::put('/dashboard/{id}', [
        'uses' => 'DashboardController@update',
        'as'   => 'mahasiswa.update',
    ]);

    Route::delete('/dashboard/{id}', [
        'uses' => 'DashboardController@destroy',
        'as'   => 'mahasiswa.destroy',
    ]);
});

我的视图上不断出现“未定义变量:mahasiswa”

有人知道原因吗?

3 个答案:

答案 0 :(得分:1)

您正在传递名为frame的数据,并尝试在$mahasiswa上进行迭代,因此可以将刀片更改为:

@foreach($frame as $mhs)
   <tr>
       <td>{{$mhs->id}}</td>
       <td>{{$mhs->nim}}</td>
       <td>{{$mhs->nama}}</td>
       <td>{{$mhs->alamat}}</td>
       <td>{{$mhs->fakultas}}</td>
       <td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit">&#xE254;</i></a>
           <a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete">&#xE872;</i></a>
       </td>
       </tr>
@endforeach

或在您的控制器中:

public function index(Request $request)
{
    $mhs = Dashboard::orderBy('id', 'desc')->paginate(5);

    return view('frame')->with('mahasiswa', $mhs);
}

答案 1 :(得分:1)

尝试这种方式:

首先在控制器中将您的数据库查询结果分配给 $ mahasiswa

use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class DashboardController extends Controller
{

public function index(Request $request)
{
    $mahasiswa = Dashboard::orderBy('id', 'desc')->paginate(5);

    return view('frame', compact('mahasiswa'));
}

然后在视图中:

@foreach($mahasiswa as $mhs)
   <tr>
       <td>{{$mhs->id}}</td>
       <td>{{$mhs->nim}}</td>
       <td>{{$mhs->nama}}</td>
       <td>{{$mhs->alamat}}</td>
       <td>{{$mhs->fakultas}}</td>
       <td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit">&#xE254;</i></a>
           <a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete">&#xE872;</i></a>
       </td>
   </tr>
@endforeach

它将消除该错误。

我使用了返回视图(“框架”,紧凑型(“ mahasiswa”)); ,即紧凑,而不是带有。我认为对于初学者来说,它更干净,也更容易混淆。

答案 2 :(得分:1)

它仍然可以这样工作:

use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class DashboardController extends Controller
{

public function index(Request $request)
{
    $data['mahasiswa'] = Dashboard::orderBy('id', 'desc')->paginate(5);

    return view('frame',$data);
}

视图

@foreach($mahasiswa as $mhs)
   <tr>
       <td>{{$mhs->id}}</td>
       <td>{{$mhs->nim}}</td>
       <td>{{$mhs->nama}}</td>
       <td>{{$mhs->alamat}}</td>
       <td>{{$mhs->fakultas}}</td>
       <td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit">&#xE254;</i></a>
           <a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete">&#xE872;</i></a>
       </td>
       </tr>
@endforeach