跟踪多个布拉维尔(Laravel)的多个布尔人

发布于 2025-02-13 17:57:01 字数 962 浏览 1 评论 0原文

我的关系表的迁移代码:

Schema::create('users_games', function (Blueprint $table) {
        $table->bigInteger('user_id')->unsigned();
        $table->index('user_id');
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

        $table->bigInteger('game_id')->unsigned();
        $table->index('game_id');
        $table->foreign('game_id')
            ->references('id')
            ->on('games')
            ->onDelete('cascade');

        $table->boolean('liked')->default(false);
        $table->boolean('played')->default(false);
        $table->boolean('wishlisted')->default(false);

        // composite primary key
        $table->primary(['user_id', 'game_id']);
    });

基本上,用户可以标记游戏,播放或添加到愿望清单中。我想在同一表中跟踪所有这些动作。

我想在用户和游戏的Elloquent模型中代表相同的内容,但是我不知道该怎么做。谁能告诉我各自模型中应该看起来像是什么样的这些属性?

My migration code for relation table:

Schema::create('users_games', function (Blueprint $table) {
        $table->bigInteger('user_id')->unsigned();
        $table->index('user_id');
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

        $table->bigInteger('game_id')->unsigned();
        $table->index('game_id');
        $table->foreign('game_id')
            ->references('id')
            ->on('games')
            ->onDelete('cascade');

        $table->boolean('liked')->default(false);
        $table->boolean('played')->default(false);
        $table->boolean('wishlisted')->default(false);

        // composite primary key
        $table->primary(['user_id', 'game_id']);
    });

basically a user can mark a game like, played or add to wishlist. I want to track all those actions in the same table.

I want to represent the same in Elloquent Model for users and games, however I dont know how to. can anyone tell me how these properties inside respective models should look like please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2025-02-20 17:57:02

在您的用户模型中:

<?php
namespace App\Models;
class User extends Model
{
    public function games()
    {
        return $this->hasMany(Game::class);
    }
}

在您的游戏模型中:

<?php
namespace App\Models;
class Game extends Model
{
    public function user()
    {
        return $this->belongsToMany(User::class);
    }
}

然后,您可以从雄辩的第一个游戏中获得播放的属性的价值:

$user = auth->user()
$user->games->first()->liked

in your User Model:

<?php
namespace App\Models;
class User extends Model
{
    public function games()
    {
        return $this->hasMany(Game::class);
    }
}

in your Game model:

<?php
namespace App\Models;
class Game extends Model
{
    public function user()
    {
        return $this->belongsToMany(User::class);
    }
}

then you can get the value of played property from the first game in the Eloquent like this:

$user = auth->user()
$user->games->first()->liked
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文