如何在laravel中插入具有多类别和多列不同类别的帖子? [英] How to insert a post with multi category and with multi column deferent category in laravel?

查看:92
本文介绍了如何在laravel中插入具有多类别和多列不同类别的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是新来的laravel,我的项目有问题.我有2张桌子:

Hello all I am a new laravel, I have problem with my project. I have 2 table:

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

例如:如果我将帖子插入帖子数据库title = Jonh并选择两个类别名称(PHP,Laravel),结果将是两列,例如posts(id = 1 title(jonh)category_id (1),id = 2 title(jonh)category_id(2)),但仍然无法正常工作,我尝试在google和youtube中找到关注对象,但仍无法正常工作,这是我的代码:

Ex:I want result like, if I am insert a post into posts database title=Jonh and chosse two category name(PHP,Laravel), and the result will two column like posts(id=1 title(jonh) category_id(1), id=2 title(jonh) category_id(2)) but it still not work I try to find in google and youtube follow and it not work, and here is my code:

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();
    }

帮助请

推荐答案

您的帖子"和类别"之间的关系不正确.一个帖子应具有一个或多个类别,而一个类别应与零个或多个帖子相关联.

Your relationship between Post and Category is incorrect. One post should have one or more categories and one category should be associated with zero or more posts.

帖子有很多类别
类别有很多帖子

下面我们有表结构:

-----帖子-----
ID INT
标题VARCHAR(100)
正文

----- posts -----
id INT
title VARCHAR(100)
body TEXT

----- category_post -----
category_id INT
post_id INT

----- category_post -----
category_id INT
post_id INT

-----类别-----
ID INT
名称VARCHAR(100)

----- categories -----
id INT
name VARCHAR(100)

您应该做的是:

1-创建到category_post表的新迁移

1 - create a new migration to category_post table

<?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->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
    $table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
});

2-在雄辩的模型中更改职位"和类别"之间的关系.

2 - change the relationship between Post and Category inside your Eloquent models.

<?php

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

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

现在您可以执行以下操作:

Now you can do something like this:

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

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

public function store(Request $request)
{
    $post = new Post();
    $post->title = $request->name;
    $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中插入具有多类别和多列不同类别的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆