首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Laravel 5.2中使用eloquent而不是query

在Laravel 5.2中使用eloquent而不是query
EN

Stack Overflow用户
提问于 2016-03-15 23:51:55
回答 1查看 142关注 0票数 2

我提出了一个问题,但我并不满意,因为我想用雄辩的口才来完成它!

下面是查询:

代码语言:javascript
复制
Tournament::leftJoin('category_tournament', 'category_tournament.tournament_id', '=', 'tournament.id')
        ->leftJoin('category_tournament_user', 'category_tournament_user.category_tournament_id', '=', 'category_tournament.id' )
        ->where('category_tournament_user.user_id', '=',$this->id )
        ->select('tournament.*')
        ->distinct()
        ->get();

迁移:

代码语言:javascript
复制
Schema::create('tournaments', function(Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('categories', function(Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('category_tournament', function(Blueprint $table) {
    $table->increments('id');
    $table->integer('category_id');
    $table->integer('tournament_id');
    $table->timestamps();

    $table->foreign('tournament_id')
            ->references('id')
            ->on('tournament')
            ->onDelete('cascade');

        $table->foreign('category_id')
            ->references('id')
            ->on('category')
            ->onDelete('cascade');
});

Schema::create('category_tournament_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('category_tournament_id')->unsigned()->index();
        $table->foreign('category_tournament_id')
            ->references('id')
            ->on('category_tournament')
            ->onDelete('cascade');


        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

        $table->unique(array('category_tournament_id', 'user_id'));

        $table->boolean('confirmed');

        $table->timestamps();
        $table->softDeletes();
        $table->engine = 'InnoDB';


    });

模型

代码语言:javascript
复制
class Tournament extends Model
{
    public function categories()
    {
        return $this->belongsToMany(Category::class);
    }

    public function categoryTournaments()
    {
        return $this->hasMany(CategoryTournament::class);
    }
}

class Category extends Model
{
    public function tournaments()
    {
        return $this->belongsToMany(Tournament::class);
    }

    public function categoryTournament()
    {
        return $this->hasMany(CategoryTournament::class);
    }
}

class CategoryTournament extends Model
{
    protected $table = 'category_tournament';

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function tournament()
    {
        return $this->belongsTo(Tournament::class);
    }

    public function users()
    {
        return $this->belongsToMany(User::class, 'category_tournament_user');
    }
}

class User extends Authenticatable
{
    public function categoryTournaments()
    {
        return $this->belongsToMany(CategoryTournament::class, 'category_tournament_user');
    }
}

查询是有效的,我只想知道我应该如何使用Eloquent来完成它,因为我不能自己完成它:(

你知道怎么做吗?

EN

回答 1

Stack Overflow用户

发布于 2016-03-16 00:11:52

首先,您必须将外键添加到您的迁移表中,将它们设置为无符号非常有用,因为您可能永远不会有任何负it:

代码语言:javascript
复制
$table->integer('tournament_id')->unsigned();
$table->foreign('tournament_id')->references('id')->on('tournaments');

既然您已经有了Model的关系,那么您应该能够使用eloquent来获取内容。

有关Eloquent:https://laravel.com/docs/5.0/eloquent的更多信息,请访问此页面

应该是这样的:

代码语言:javascript
复制
$tournaments = Tournament::with('category_tournament')
   ->with('category_tournament_user')    
   ->where('category_tournament_user.user_id', '=',$this->id ) 
   ->distinct()
   ->get();

之后,您可以使用$tournaments的内容:

代码语言:javascript
复制
$tournaments->category_tournament->name;

希望我没有错过任何关键的步骤,但我认为它应该通过做这些改变来工作。

编辑:

https://laravel.com/docs/5.1/eloquent-relationships#eager-loading

当访问有说服力的关系作为属性时,关系数据是“延迟加载”的。这意味着直到您第一次访问该属性时,关系数据才会实际加载。但是,Eloquent可以在您查询父模型时“立即加载”关系。预加载缓解了N+1查询问题。为了说明N+1查询问题,请考虑一个与Author:

相关的图书模型

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36016105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档