gpt4 book ai didi

laravel - 如何在 laravel 中插入多类别和多列不同类别的帖子?

转载 作者:行者123 更新时间:2023-12-05 08:27:32 27 4
gpt4 key购买 nike

大家好,我是一个新的 Laravel,我的项目有问题。我有 2 个表:

posts: id title category_id
category: id name(PHP, Laravel, Ruby)

例如:我想要这样的结果,如果我在帖子数据库中插入一个帖子 title=Jonh 并选择两个类别名称(PHP、Laravel),结果将像帖子(id=1 title(jonh) category_id 这样的两列(1), id=2 title(jonh) category_id(2)) 但它仍然不起作用

View(create.blade.php)
{!!Form::open(array('route' => 'store', 'method' => 'POST'))!!}

{{Form::text('title')}}<br>

@foreach($category as $categories)
{{Form::label('category',$categories->name)}}
{{Form::checkbox('category_id', $categories->id) }}
@endforeach
<br>
{{Form::submit('submit')}}
{!!Form::close()!!}

Controller(PostsController.php)
public function create()
{
$category = Category::all();
return view('create',compact('category'));
}
public function store(Request $request)
{
$post = new Posts;
$post->title = $request['title'];
$post->category_id = $request['category_id'];
$post->save();
}

请帮忙

最佳答案

我认为 Post 和 Category 之间的关系不正确。一篇文章属于零个或多个类别(您可以在应用层限制为一个或多个)并且一个类别可以与零个或多个帖子相关联。这种类型的关系称为多对多

发布 belongsToMany 类别
Category belongsToMany Post

下面我们有表结构:

----- 帖子 -----
id 整数
标题 VARCHAR(100)
正文

----- category_post -----
category_id 整数
post_id 整数

----- 类别 -----
id 整数
名称 VARCHAR(100)

你应该做的是:

1 - 创建一个新的迁移到 category_post 表

<?php

// migrations/****_**_**_******_create_posts_table.php
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 100);
$table->text('body');
});

// migrations/****_**_**_******_create_categories_table.php
Schema::create('categories', function(Blueprint $table) {
$table->increments('id');
$table->string('name', 100);
});

// migrations/****_**_**_******_create_category_post_table.php
Schema::create('category_post', function(Blueprint $table) {
$table->integer('category_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->primary(['category_id', 'post_id']);
$table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
});

2 - 在 Eloquent 模型中更改 Post 和 Category 之间的关系。

<?php

// Post.php
public function categories()
{
return $this->belongsToMany(Category::class);
}

// Category.php
public function posts()
{
return $this->belongsToMany(Post::class);
}

现在你可以这样做:

// PostController.php
public function create()
{
$categories = Category::all();

return view('posts.create', compact('categories'));
}

public function store(Request $request)
{
$this->validate($request, [
// Validate the max number of characters to avoid database truncation
'title' => ['required', 'string', 'max:100'],
'body' => ['required', 'string'],
// The user should select at least one category
'categories_id' => ['required', 'array', 'min:1'],
'categories_id.*' => ['required', 'integer', 'exists:categories,id'],
]);

$post = new Post();
$post->title = $request->title;
$post->body = $request->body;
$post->categories()->attach($request->categories_id);

return redirect()->route('posts.index');
}

// views/posts/create.blade.php
<select name="categories_id" multiple>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>

关于laravel - 如何在 laravel 中插入多类别和多列不同类别的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38746613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com