您好,我在理解如何在 Laravel 的 datatable.net 中使用数据表时遇到问题。
这是我的 Controller 来生成 View
public function hospital()
{
$view = View::make('doctor.hospital', array());
return $view;
}
这是我的数据表 Controller
public function allHospitalList()
{
$hospitals = HospitalList::hospitalsList();
return $hospitals;
}
这是我的模型
<?php
class HospitalList extends Eloquent {
protected $table = 'hospital_list';
public static function hospitalsList()
{
$data = HospitalList::join('Cities', 'Cities.id', '=', 'hospital_list.Address3')
->join('Province', 'Province.id', '=', 'hospital_list.Address4')
// ->paginate(10);
->get();
return $data;
}
}
在我的 route
Route::get('hospital',
array(
'uses'=>'DoctorController@hospital'
)
);
/* datatables */
Route::get('hospitallist',
array(
'as' => 'dt_hospital_all',
'uses'=>'DatatableController@allHospitalList'
));
这是我在 View 中的表格
<table class="table table-hover radius" id="hospitaList">
<thead>
<tr>
<th>
</th>
<th>
Name
</th>
<th>
Type
</th>
<th>
Contact
</th>
</tr>
</thead>
<tbody>
<?php
foreach ($hospitals as $hospital)
{?>
<tr>
<td>
<!-- <span class="row-details row-details-close"></span> -->
<button type="button" class="btn red"><i class="fa fa-plus-square-o"></i>
</button>
</td>
<td>
{{ $hospital->name }}</br>
{{ $hospital->address1 }} {{ $hospital->address2 }}
<?php
if($hospital->address1 != '' || $hospital->address2 != '') echo "</br>";
?>
{{ $hospital->City }}, {{ $hospital->province }}
</td>
<td>
{{ $hospital->type }}
</td>
<td>
{{ $hospital->contact }}
</td>
</tr>
<?php
}
?>
</tbody>
</table>
这是我在 View 上的 JavaScript
var url = "{{ URL::route('dt_hospital_all') }}";
$(document).ready(function() {
$('#hospitalList').dataTable( {
"processing": true,
"serverSide": true,
"ajax": url
} );
} );
我收到错误
Undefined variable: hospitals (View: /Applications/AMPPS/www/docsearch/app/views/doctor/hospital.blade.php)
最佳答案
现在,您引用的是一个不存在的变量,$hospitals
。
您需要向您的 View 提供$hospitals
:
View::make('doctor.hospital')->with('hospitals', Hospital::all())
这假设您有一个Hospital
模型。
关于javascript - laravel 和数据表无法集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24584470/