我正在使用Laravel 5.5,并且在浏览器中打开了index.blade.php文件,但出现“未找到”错误。当我删除文件中的PHP时,它便可以工作,并可以在浏览器中成功加载。
因此,我开始认为我的PHP已经过时,并且我对“ data-postid”感到非常困惑。
index.blade.php
<?php foreach ($posts as $post) ?>
<div class="post" data-postid = " {{ $post->id }} ?>">
<a href="#"><h3><<?php {{$post->title}} ?></h3></a>
<h6>Posted on <?php {{$post->created_at}} by {{$post->user->name}} ?></h6>
<p><?php {{$post->body}} ?></p>
<div class="interaction">
<a href="#">Likes</a>
<a href="#">Dislikes</a>
<hr>
</div>
</div>
<?php endforeach; ?>
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/post', 'PostController@index');
Route::get('/home', 'HomeController@index')->name('home');
Auth::routes();
PostController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function index ()
{
$posts = Post::orderBy('created_at', 'desc')->get();
return view('post.index', ['posts' => $posts]);
}
}
答案 0 :(得分:0)
您需要在web.php
文件中创建路由
Route::get('/index', function(){
$posts = App\Post::all();
return view('index',compact('posts'));
});
现在在您的index.blade.php
中,您也可以使用刀片指令将代码放入此文件中
@foreach($posts as $post)
<div class="post" data-postid="{{ $post->id }}">
<a href="#"><h3>{{ $post->title }}</h3></a>
<h6>Posted on {{ $post->created_at }} by {{ $post->user->name }}</h6>
<p>{{ $post->body }}</p>
<div class="interaction">
<a href="#">Likes</a>
<a href="#">Dislikes</a>
<hr>
</div>
</div>
@endforeach
谢谢。