How to insert a post with multi category and with multi column deferent category in laravel?

喜夏-厌秋 提交于 2019-12-12 09:24:02

问题


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)

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

Help pls


回答1:


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.

Post hasMany Category
Category hasMany Post

Below we have the tables structure:

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

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

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

What you should do is:

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 - 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>


来源:https://stackoverflow.com/questions/38746613/how-to-insert-a-post-with-multi-category-and-with-multi-column-deferent-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!