【问题标题】:Laravel - How to get posts from db with user data?Laravel - 如何从数据库中获取包含用户数据的帖子?
【发布时间】: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 可以吗?

【问题讨论】:

标签: php laravel eloquent


【解决方案1】:

是的,这是可能的,阅读Laravel Eloquent relationships documentation 将是一个不错的起点。 Laravel 的文档真的很不错。

除此之外......

您的迁移不正确,因为它缺少存储user_id 关系id 的字段。所以你会想要添加它。

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteter('user_id');
        $table->foreign('user_id')->references('id')->on('users');
    });
}

然后在您的User 模型中,您需要一个返回与User 相关的Posts 的函数。在这种情况下,它是一个hasMany 关系,因为User 可以有many Posts

User.php

public function posts()
{
    // A User has many posts
    return $this->hasMany(Post::class);
}

然后在Post模型上定义反函数得到它所属的User

Post.php

public function User()
{
    // A Post belongs to a single User
    return $this->belongsTo(User::class);
}

现在您在模型上定义了migrationrelationships,您可以继续使用它们。

假设您定义了以下路由(使用 Laravel 8 语法)。

Route::get('/posts', [PostController::class, 'index']);

调用以下Controllerindex函数。

PostController.php

class PostController
{
    public function index()
    {
        // Get the Posts and eager load the related User
        $posts = Post::with('user')->get();
        return view('posts.index', compact('posts'));
    }
}

在您的刀片视图中,您可以访问PostUser,就像它是Post 的属性一样。

posts/index.blade.php

@foreach ($posts as $post)
    <p>{{ $post->id }}</p>
    <p>{{ $post->user->name }}</p>
@endforeach

如果你想要randomPosts,那么你可以使用inRandomOrderlimit来获得一些,例如随机获得10Posts

$posts = Post::inRandomOrder()->with('user')->limit(10)->get();

【讨论】:

  • 非常感谢,你救了我。
【解决方案2】:

您必须在Post.php 中添加belongsTo 关系,例如:

public function users(){
    return $this->belongsTo(User::class);
}

您可以访问刀片文件中的用户信息,例如:

@foreach($posts as $post) {
  <div>{{ $post->id }}</div>
  // I want to get the post's owner data.
  <h1>{{ $post->user()->get()->name }}</h1>
}

【讨论】:

  • 谢谢你 :)
【解决方案3】:

您必须在模型上建立关系。 就像上面所说的一样。 https://laravel.com/docs/8.x/eloquent-relationships#one-to-many

您还可以进行预先加载以加载用户帖子上的关系。

$posts = Post::with('user')->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2017-11-26
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2016-12-30
    相关资源
    最近更新 更多