在Laravel 5中寻找一种简单的方法来完成以下操作。
想象一下这些模型和接口
比赛将与is参与者有多到多的关系,他们既可以是用户,也可以是团队。因此,我需要competition_participant枢轴表有以下列来定义模型
但是我如何在竞争模型上写一个关系,以便它知道应该从数据库中获取哪个模型,同时返回一个混合模型的集合或接口类型的集合?
发布于 2016-01-15 16:29:01
对于哪些表应该得到哪些列,您有一些困惑。
参与者表需要是一个用于管理users
和teams
的多态关系的表。它至少接收一个particpatable_id
和particpatable_type
,以及一个id
和timestamps()
。
然后,您需要一个名为competition_participant
的额外表,它将管理属于competitions
和多态表participants
之间的多个表。
这将允许您将您的participants
与您的competitions
联系起来。这样,您就可以通过竞争来抓取所有参与者,调用它上的$participant->participatable
属性,根据参与者的类型返回App\User
或App\Team
。
这都是经过测试的。
迁徙
public function up()
{
Schema::create('competitions', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('teams', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('participants', function(Blueprint $table) {
$table->increments('id');
$table->integer('participatable_id');
$table->string('participatable_type');
$table->timestamps();
});
Schema::create('competition_participant', function(Blueprint $table) {
$table->integer('competition_id');
$table->integer('participant_id');
});
}
模型
class Competition extends Model
{
public function participants()
{
return $this->belongsToMany(Participant::class);
}
}
class Participant extends Model
{
public function participatable()
{
return $this->morphTo();
}
public function competitions()
{
return $this->belongsToMany(Competition::class);
}
}
class Team extends Model
{
public function participants()
{
return $this->morphMany(Participant::class, 'participatable');
}
}
class User extends Authenticatable
{
public function participants()
{
return $this->morphMany(Participant::class, 'participatable');
}
}
种子
public function run()
{
$faker = Faker\Factory::create();
// Seed Users Table
DB::table('users')->delete();
$users = [];
for($i = 0; $i < 100; $i++) {
$users[] = [
'name' => $faker->name,
'email' => $faker->email,
'password' => Hash::make($faker->password),
'created_at' => new DateTime,
'updated_at' => new DateTime
];
}
DB::table('users')->insert($users);
// Seed Teams Table
DB::table('teams')->delete();
$teams = [];
for($i = 0; $i < 20; $i++) {
$teams[] = [
'name' => 'Team ' . ucwords($faker->domainWord),
'created_at' => new DateTime,
'updated_at' => new DateTime
];
}
DB::table('teams')->insert($teams);
// Seed Participants Table
DB::table('participants')->delete();
// Insert some of our users as participants
$users = App\User::limit(20)->orderByRaw('rand()')->get();
foreach($users as $user) {
$user->participants()->create([]);
}
// Insert some of the teams as participants
$teams = App\Team::limit(10)->orderByRaw('rand()')->get();
foreach($teams as $team) {
$team->participants()->create([]);
}
// Seed Competitions Table
DB::table('competitions')->delete();
$competitions = [];
for($i = 0; $i < 10; $i++) {
$competitions[] = [
'name' => $faker->company,
'created_at' => new DateTime,
'updated_at' => new DateTime,
];
}
DB::table('competitions')->insert($competitions);
// Seed Competitions Participants Relationships
DB::table('competition_participant')->delete();
// Sign up each participant to 3 random competitions
$participants = App\Participant::all();
$competitions = App\Competition::all();
foreach($participants as $participant) {
$participant->competitions()->sync($competitions->shuffle()->take(3));
}
}
用法
$competition = App\Competition::with('participants')->has('participants')->first();
foreach($competition->participants as $participant) {
echo get_class($participant->participatable); // Will output either App\User or App\Team
echo "<br />";
}
发布于 2016-01-15 14:47:59
这只是来自关于许多到多个多态关系的Laravel文档的复制粘贴。
为了帮助您更好地理解,这是您的模型与Laravel文档中的模型之间的映射:
users == posts
teams == videos
competitions == tags
competitionables == tags
这就是您应该如何实现多态关系的方式,记住,每当您感到困惑时,只需查看文档以及模型与文档模型之间的映射:
表:
users
id - integer
name - string
teams
id - integer
name - string
competitions
id - integer
name - string
competitionables
competition_id - integer
competitionable_id - integer
competitionable_type - string
模型结构
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get all of the Competition for the user.
*/
public function competitions()
{
return $this->morphToMany('App\Competition', 'competitionable');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
/**
* Get all of the Competition for the Team.
*/
public function competitions()
{
return $this->morphToMany('App\Competition', 'competitionable');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Competition extends Model
{
/**
* Get all of the users that are assigned to this Competition.
*/
public function users()
{
return $this->morphedByMany('App\User', 'competitionable');
}
/**
* Get all of the Teams that are assigned to this Competition.
*/
public function teams()
{
return $this->morphedByMany('App\Video', 'competitionable');
}
}
检索关系
$user = App\User::find(1);
foreach ($user->competitions as $competition) {
//
}
或者另一种方式
$competition = App\Competition::find(1);
foreach ($competition->users as $user) {
// do something with your users
}
foreach ($competition->teams as $team) {
// do something with your teams
}
https://stackoverflow.com/questions/34809469
复制相似问题