【发布时间】:2017-07-08 04:19:40
【问题描述】:
我是 Laravel php 框架的新手。我对这个框架所做的是创建路由、控制器和模型,然后我在数据库上创建了一些记录(不是通过 Laravel,我直接在数据库中完成)但是当我尝试获取它们并显示它时视图我什么都看不到,我不知道是我没有从数据库中获取对象还是问题只是我没有显示。
这是我的路线:
Route::get('questions/show', 'questions\ShowQuestionsController@show');
控制器:
namespace App\Http\Controllers\questions;
use App\User;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class ShowQuestionsController extends Controller
{
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function show()
{
$questions = DB::table('questions')->get();
return view('questions/showquestions', ['questions' => $questions]);
}
}
观点:
showquestions!!!!!!!!!!!!111111!!!
@foreach ($questions as $question)
<p>This is user {{ $question->statement }}</p>
@endforeach
foreach ($questions as $question) {
echo $question->id;
}
echo $questions;
还有模特:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Question extends Model
{
//
protected $table = 'questions';
}
在日志和控制台上我没有收到任何错误消息
编辑:
视图 v2:
showquestions!!!!!!!!!!!!111111!!!
<?php dd($questions);
@foreach ($questions as $question)
<p>This is user {{ $question->statement }}</p>
@endforeach
foreach ($questions as $question) {
echo $question->id;
}
echo $questions;
这就是我使用 dd($questions) 得到的结果:
Collection {#165 ▼
#items: array:5 [▼
0 => {#171 ▼
+"id": 1
+"statement": "Pregunta 1"
+"num": 1
+"created_at": null
+"updated_at": null
}
1 => {#173 ▼
+"id": 2
+"statement": "Pregunta 2"
+"num": 2
+"created_at": null
+"updated_at": null
}
2 => {#174 ▶}
3 => {#175 ▶}
4 => {#176 ▶}
]
}
【问题讨论】:
-
当我访问视图时,我只能看到显示问题!!!!!!!!!!!!111111!!!留言
-
在你的第一个
@foreach夹在 -
转储你的结果:
dd($questions)并告诉我们它在你的控制器中查询后立即打印的内容 -
@Chris 我试过了,但还是一样
-
@proktovief 它不应该,当你添加它时,控制器应该转储
$questions的值或者应该打印空白屏幕
标签: php database laravel laravel-5.4