posts with static categories

Published 4 months ago by jkew

Hi, Im a beginner and I have a doubt. When a user is in a page to create a new post he can select categories for the post using a select menu. These categories should be stored in a database table categories and there is a relation many to many between posts and categories, because a post can have many categories. But the categories are static, there are just 10, and its not possible in the system to create new categories.

But to load the categories in the database so the user can have access to them in the page of creating a new post, there is some way to do this in laravel or its better just insert manually in the database?

Relevant code for this context:

// Category Model:

class Category extends Model
{
    public function posts(){
        return $this->belongsToMany('App\Post’);
    }
}
// Post model

class Post extends Model
{
    public function categories(){
        return $this->belongsToMany('App\Category');
    }

}
Migration Post:

class CreatePost extends Migration
{

    public function up()
    {
        Schema::create(‘posts’, function (Blueprint $table) {
            $table->increments('id');
            $table->string(‘name’);
            …
            $table->timestamps();
        });
    }
Migration Category:

<?php

class CreateCategoriesTable extends Migration
{

    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }
}
// post controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{

    public function index()
    {
    }

    public function create()
    {
        return view(‘app.createPost’);
    }


    public function store(Request $request)
    {
        dd($request->all());

        $this->validate($request, [
           ‘post_name’ => 'required|max:255',
           ‘post_categories’ => 'required',
            ‘post_date' => 'required',

        ]);
}
Best Answer (As Selected By jkew)
Snapey

Are you wondering how to fill the categories table with 10 values ?

You can create a seeder.

php artisan make:seeder CategorySeeder

database/seeds/CategorySeeder.php

<?php

use Illuminate\Database\Seeder;

use App\Category;

class CategorySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Category::create(['name'=>'category1']);
        Category::create(['name'=>'category2']);
        Category::create(['name'=>'category3']);
        Category::create(['name'=>'category4']);
        Category::create(['name'=>'category5']);
        Category::create(['name'=>'category6']);
        Category::create(['name'=>'category7']);
        Category::create(['name'=>'category8']);
        Category::create(['name'=>'category9']);
        Category::create(['name'=>'category10']);

    }
}

database/seeds/DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
                CategorySeeder::class,
                ]);
    }
}

Then to seed the database;

php artisan db:seed

Doing it this way allows you to start with a clean database and setup the static categories automatically. The category names are also saved in your code base rather than being 'remembered'

Snapey
Snapey
4 months ago (896,135 XP)

When user creates post, you have to get all categories $categories = Category::all(); and send them to the view so that they can populate a dropdown or input field.

Since you want many categories, you need a UI component that allows multiple categories to be selected.

You could use the popular select2 javascript package. Have a look at http://www.codebyjigs.com/select2-laravel/ and the Tagging example. There are plenty of other examples around using select2

jkew
jkew
4 months ago (1,140 XP)

Tks, but to store the categories in the database in order to allow the user when is creating a new post, in the create new post page, can get and select the available categories what is the best approach to do that? Insert in the database manually or use migrations or someting? Because in the system is not possible to insert new categories, there are just 10 static categories.

Snapey
Snapey
4 months ago (896,135 XP)

Are you wondering how to fill the categories table with 10 values ?

You can create a seeder.

php artisan make:seeder CategorySeeder

database/seeds/CategorySeeder.php

<?php

use Illuminate\Database\Seeder;

use App\Category;

class CategorySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Category::create(['name'=>'category1']);
        Category::create(['name'=>'category2']);
        Category::create(['name'=>'category3']);
        Category::create(['name'=>'category4']);
        Category::create(['name'=>'category5']);
        Category::create(['name'=>'category6']);
        Category::create(['name'=>'category7']);
        Category::create(['name'=>'category8']);
        Category::create(['name'=>'category9']);
        Category::create(['name'=>'category10']);

    }
}

database/seeds/DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
                CategorySeeder::class,
                ]);
    }
}

Then to seed the database;

php artisan db:seed

Doing it this way allows you to start with a clean database and setup the static categories automatically. The category names are also saved in your code base rather than being 'remembered'

Please sign in or create an account to participate in this conversation.