我试图在Laravel 5.6.9中的视图中显示数据,但我一直收到此错误。
错误
代码段
TodosController
<div class="title m-b-md">
<?php $__currentLoopData = $todos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $todo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php echo e($todo->todo); ?>
<br>
浏览器发出此错误
{{1}}
答案 0 :(得分:2)
在您的控制器中,您必须删除return view('todos')->with('todos', $todos);
变量周围的单引号:
var baseUrl = $("#base_url").val();
var currentLeave = 0;
var currentUnit ="";
var currentEmp =0;
var currentUsed=0;
$(document).ready(function () {
$("#start_date").datepicker({
format: 'yyyy-mm-dd',
autoclose: true,
todayHighlight: true
})
$("#end_date").datepicker({
format: 'yyyy-mm-dd',
autoclose: true,
todayHighlight: true
})
$('#example').DataTable({
buttons: [
{
extend: 'print',className: "btn btn-default btn-sm btn-success",
title: window.reportTitle,
},
{
extend: 'excelHtml5',className: "btn btn-default btn-sm btn-primary",
title: 'Leave Report',
}
],
"bLengthChange": false,
"bFilter": false,
responsive: true,
"bSort": false,
dom: "Bfrtip",
destroy: true,
searching: !0
});
$('#data1').DataTable({
buttons: [
{
extend: 'print',className: "btn btn-default btn-sm btn-success",
title: window.reportTitle,
},
{
extend: 'excelHtml5',className: "btn btn-default btn-sm btn-primary",
title: 'Leave Report',
}
],
"bLengthChange": false,
"bFilter": false,
responsive: true,
"bSort": false,
dom: "Bfrtip",
destroy: true,
searching: !0
});
});
function approveLeave(id,unit,emp_id,used_leave) {
$("#approveModal").modal('toggle');
currentLeave = id;
currentUnit= unit;
currentEmp= emp_id;
currentUsed = used_leave;
}
function approveNow() {
var startDate = $("#start_date").val();
var endDate = $("#end_date").val();
if (startDate != "" && endDate != "") {
if (startDate < endDate) {
var csrf_token = $('meta[name="csrf-token"]').attr('content');
$.ajax({
url: baseUrl + "/approve-leave",
method: "POST",
data: {id:currentLeave, _token: csrf_token, status: 'Approved', start_date: startDate,unit:currentUnit,emp_id:currentEmp,used_leave:currentUsed,return_date: endDate},
dataType: 'json',
success: function (data) {
if (data.status == "success") {
swal({
title: 'Success!',
text: 'Data has been updated',
type: 'success'
})
window.location.reload();
} else {
swal({
title: 'Failled',
text: data.err_msg,
type: 'error'
})
}
window.location.reload();
},
error: function () {
swal({
title: 'Oops...',
text: 'Something went wrong!',
type: 'error'
})
}
});
} else {
swal({
title: 'Failed',
text: 'Start Date should less than End Date',
type: 'error'
})
}
} else {
swal({
title: 'Failed',
text: 'Start date and end date required',
type: 'error'
})
}
}
function rejectLeave(id) {
var csrf_token = $('meta[name="csrf-token"]').attr('content');
swal({
title: 'Enter rejection reason',
input: 'textarea',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Reject',
cancelButtonText: 'Cancel',
confirmButtonClass: 'btn btn-success',
cancelButtonClass: 'btn btn-danger',
buttonsStyling: false,
allowOutsideClick: false,
showLoaderOnConfirm: true,
inputPlaceholder: 'This rejection will be sent via email to the Employee',
preConfirm: function (textarea) {
return new Promise(function (resolve, reject) {
if (textarea == "") {
reject("Enter a comment for rejection");
} else {
$.ajax({
url: "reject-leave",
type: "POST",
dataType: "json",
data: {id: id, _token: csrf_token, comment: textarea, status: 'Rejected'},
success: function (data) {
if (data.status == "success") {
swal({
title: 'Success!',
text: 'you have rejected this employee!',
type: 'success'
})
} else {
swal({
title: 'Oops...',
text: data.err_msg,
type: 'error'
})
}
window.location.reload();
},
error: function () {
swal({
title: 'Oops...',
text: 'Something went wrong, please try again later!',
type: 'error'
})
}
});
}
})
}
}).then(function () {
});
}
答案 1 :(得分:0)
您应该更改控制器代码,如:
namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller
{
public function index() {
$todos = Todo::get();
return view('todos',compact('todos'));
}
}
答案 2 :(得分:0)
更明智的方法是使用紧凑型。 Compact是一个PHP函数,它创建一个包含变量及其值的数组。
返回视图时,我们可以轻松使用compact传递一些数据。
可以像这样使用契约:
$data = Data::all();
return view('viewname')->with(compact('data'));
所以在你的剧本中:
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with(compact('todos'));
}
}
如果您希望按照首先尝试的方式进行,您应该这样做:
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with('todos', $todos);
}
}
请注意变量$ todos周围没有撇号。