拉维尔8号我需要一些帮助..我不知道错误在哪里?
当我在修补程序“$ ->>> ->”标签中写到"null“时,我得到了”空“;
Tinker (她的是错误):?

Creating_tags_tabels.php:

Tags.php:

Articles.php:

DB表-> tablePlus "article_tag“:

DB表-> tablePlus "tags":

在下一集的后面,当我尝试将标签链接到"Show.blade.php“文件中的文章时,这会抛出一个错误。
发布于 2021-03-04 01:14:03
首先,Laravel的命名约定应该像table = users一样是单数,然后是model = User,所以你应该把冠词改为冠词
第二,在您的Article模型中,文章的名称属于关系更改tags to tag,因为文章属于标记,而不是标记如果它是HasMany关系,您将使用tags
第三,您似乎应该像下面这样在tinker中急切地加载模型的关系
$article = App\Models\Articles::with('tags')->first();
//then
$article->tags发布于 2021-04-25 06:20:44
php artisan make:model Articles -m
php artisan make:model Tag -m
php artisan make:migration article_tags_table
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->timestamps();
});
}
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->timestamps();
});
}
class ArticleTagsTable extends Migration
{
public function up()
{
Schema::create('article_tags', function (Blueprint $table) {
$table->id();
$table->foreignId('articles_id')
->constrained()
->onDelete('cascade');
$table->foreignId('tags_id')
->constrained()
->onDelete('cascade');
$table->unique(['article_id','tag_id']);
$table->timestamps();
});
}
}


gitlab中的项目和克隆链接link
发布于 2021-03-04 03:49:21
尝试在您的文章模型中使用。
public function tags(){
return $this->belongsToMany(Tag::class, 'article_tag', 'article_id', 'tag_id');
}https://stackoverflow.com/questions/66448583
复制相似问题