我正在尝试从数据库中检索数据并在视图中显示,但出现错误。
这是我的控制人
public function index()
{
$Page=Superior::all();
return view('Myview.Firstpage')->with('Task',$Page);
}
这是我在视图中分配的位置
<body>
<p>this is our first page </p>
{{ $Task }}
</body>
</html>
但是此任务正在创建错误,它说该任务是未定义的变量,我的整个页面看起来都是这样
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>This is our first page </title>
</head>
<body>
<p>this is our first page </p>
{{ $Task }}
</body>
</html>
Superior是我要从中检索数据的模型的名称。
我在web.php中的路由文件是
<?php
Route::get('First',function(){
return view('Myview.Firstpage');
});
我正在学习laravel
答案 0 :(得分:1)
在控制器的索引方法中
<script>
// Image Preloader version 1.0
// Copyright 2009 Bontrager Connection, LLC
// https://www.willmaster.com/
//
// Leave the next line as is.
var preload = new Array();
// List the images to be loaded, each
// image assigned to preload[#], with
// consecutive numbering starting at 0.
preload[0] = "img1.jpg";
preload[1] = "img2.jpg";
preload[2] = "img3.jpg";
preload[3] = "img4.jpg";
preload[4] = "img5.jpg";
preload[5] = "img6.jpg";
preload[6] = "img7.jpg";
preload[7] = "img8.jpg";
preload[8] = "img9.jpg";
preload[9] = "img10.jpg";
preload[10] = "img11.jpg";
// Leave the next 5 lines as they are.
var loadedimages = new Array();
for(var i=0; i<preload.length; i++) {
loadedimages[i] = new Image();
loadedimages[i].src = preload[i];
}
</script>
请记住,all()方法返回一个要在视图中循环浏览的集合。
您认为您应该具有:
<script>
paceOptions = {
ajax: true,
document: true,
eventLag: false
};
Pace.on('done', function() {
$('.p').delay(1000).animate({top: '30%', opacity: '0'}, 3000, $.bez([0.19,1,0.22,1]));
$('#preloader').delay(1500).animate({top: '-100%'}, 2000, $.bez([0.19,1,0.22,1]));
TweenMax.from(".title", 2, {
delay: 1.8,
y: 10,
opacity: 0,
ease: Expo.easeInOut
})
});
</script>
您还需要更新路线以使用控制器:
public function index()
{
return view('Myview.Firstpage')->with('tasks',Superior::all());
}
您可以访问https://laravel.com/docs/5.8/collections#method-all来了解有关收藏的更多信息。
答案 1 :(得分:1)
嗨,请尝试向您传递变量以进行如下查看:
$Tasks = Superior::all();
return view('Myview.Firstpage', compact('Tasks'));
然后在视图中使用上述注释中建议的循环。
@foreach($Tasks as $task)
{{ $task->title }}
@endforeach