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

标签 laravel laravel-5 laravel-5.2

大家好,我是一个新的 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/

相关文章:

php - Laravel 5.2 中的自定义登录验证

database - 拉维尔 4 : Working with relationships in seeds

laravel Eloquent 查询组按最后一个ID

php - Laravel 5 - 从他的所有设备上注销用户

php - 在 Laravel 中将日期和时间转换为 Jalali

php - 具有可变参数的 Laravel 5.2 命名路由用法

Laravel Eloquent 地从 json 列的值中过滤

php - php 网站开发期间的 SSL

php - Laravel 5.1 DecryptException 在 BaseEncrypter.php 第 45 行 : The payload is invalid

php - 获取数据行但相关表行数有限