大家晚上
我希望通过 category_id 外键将我的 Post Table 植入我的 Category Table 中。
但是不幸的是,我遇到了一个我无法解决的问题,这就是为什么我请求您的帮助
这是我的模特:
发布:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
/**
* @method static create(array $array)
*/
class Post extends Model
{
use SoftDeletes;
protected $fillable = ['title', 'description', 'contenu', 'image', 'published_at', 'category_id'];
/**
* Delete image from storage
*/
public function deleteImage()
{
Storage::delete($this->image);
}
// OneToMany(Inverse)
public function category()
{
return $this->belongsTo(Category::class, 'category_id', 'id');
}
// ManyToMany
public function tags()
{
return $this->belongsToMany(Tag::class);
}
/**
* Check if a post has tag.
* tags return a collection, if we want to get the id from the array
* we must use the pluck function
* @param $id
* @return bool
*/
public function hasTag($id)
{
return in_array($id, $this->tags->pluck('id')->toArray());
}
}
类别
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* @method static create(array $all)
*/
class Category extends Model
{
protected $fillable = ['name'];
// OneToMany
public function posts()
{
return $this->hasMany(Post::class);
}
}
这是我的工厂:
发布
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Category;
use App\Post;
use Carbon\Carbon;
use Faker\Generator as Faker;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
$factory->define(Post::class, function (Faker $faker) {
$image = $faker->image();
$imageFile = new File($image);
return [
'title' => 'hello',
'description' => $faker->text(100),
'contenu' => $faker->text(100),
'image' => Storage::disk('public')->putFile('posts', $imageFile),
'published_at' => $faker->date('Y-m-d'),
'category_id' => factory('App\Category')->create()->id,
// 'category_id' => $faker->numberBetween(1,5)
];
});
类别
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Category;
use Faker\Generator as Faker;
$factory->define(Category::class, function (Faker $faker) {
return [
'name' => $faker->words(5, true),
'image' => $faker->imageUrl()
];
});
我的播种者
<?php
use App\Post;
use App\Tag;
use Illuminate\Database\Seeder;
class PostTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
/**
* MANY TO MANY
*/
$posts = factory(Post::class, 15)->create(); // 1.
$tags = factory(Tag::class, 10)->create(); // 2
$posts
->each(function (Post $posts) use ($tags) {
$posts->tags()->attach(
$tags->random(rand(1, 10))->pluck('id')->toArray() // 3.
);
});
}
}
我知道通过hasMany关系,您可以这样做:
factory(User::class, 10)->create()->each(function ($user) {
$user->posts()->saveMany(factory(Posts::class, 5)->make());
});
或者这样:
//create 10 users
factory(User::class, 10)->create()->each(function ($user) {
//create 5 posts for each user
factory(Post::class, 5)->create(['user_id'=>$user->id]);
});
在用户和邮政模型相互连接的情况下:Laravel - Seeding Relationships
我也知道,如果添加此内容:
'category_id' => factory('App\Category')->create()->id,
在工厂,它将根据您要求的帖子数量生成category_id字段。
因此,这意味着如果您要求5个帖子,它将从1到5生成5个category_id号:这也意味着您数据库中的5个新类别。
然后,如果您要求50个帖子,您将获得50个新类别。
这不是我想要的。
所以我的问题是这个,
如何做到这一点,我可以添加所需的类别的所需编号,以及所需的所需的编号?>
这样两个表仍然在数据库中链接在一起。
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('category_id');
$table
->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
如果您查看我的发布工厂,则会看到此评论:
// 'category_id' => $faker->numberBetween(1,5)
我知道通过这种方式,通过生成一个随机数,其最大值等于我想要的类别数:
factory(Category::class, 5)->create();
它将为我的category_id分配一个介于1到5之间的随机数,同时,为我提供所需的期望类别数。
因此,我的帖子不会有线性的category_id(1,2,3,4,5),但是会随机排列。
我也在寻找什么。
有更好的方法吗?
答案 0 :(得分:1)
我认为您对自己的问题有答案。我只需要创建想要的 Category 对象数量(例如5)。先这样做。
factory(Category::class, 5)->create();
现在有了这些,如果这是您的首次迁移,则可以像在Post工厂模型中那样随机添加它们:
return [
'title' => 'hello',
// .. etc.
'category_id' => $faker->numberBetween(1,5)
];
但是,如果您已经具有 Category 类别的ID值,或者您可以添加更多或稍后对其进行更改,并且希望将工厂用于将来的帖子在原始迁移中,您可以获得当前的类别ID,然后将它们随机注入同一工厂的post category_id
字段中:
$cat_ids = Category::pluck('id', 'id')->toArray();
return [
'title' => 'hello',
// .. etc.
'category_id' => array_rand($cat_ids, 1)
];