【发布时间】:2021-09-19 21:10:09
【问题描述】:
我尝试使用 PHP laravel 制作 Instagram 克隆。 我想在首页显示帖子。
这是 pagesController.php:
class PagesController extends Controller
{
public function home() {
$posts = Post::all();
return view('pages.home', ['posts' => $posts]);
}
}
这是主页视图:
@foreach($posts as $post) {
<div>{{ $post->id }}</div>
// I want to get the post's owner data.
}
这是迁移后的文件:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreign('user_id')->references('id')->on('users');
});
}
}
用 laravel eloquent 可以吗?
【问题讨论】:
-
想从登录用户那里获取帖子?
-
@Atika 我想要随机帖子显示在主页上。