-1

I have been trying to google this but I still couldn't pinpoint the exact answer to the question in my head.

I have used php artisan to create a migration file to set up a table called blogs (note the plural). Previously, the table was called blog (singular) and the factory would not seed because the error shown on the terminal was that no relations found for "blogs" - which didn't make sense to me because every reference I used in the controllers, models and factories were singular. It would not let me seed until I have rolled back the migration and re-created the table as blogs in plural form.

And the strangest thing is that I have kept everything else in singular still, anyone got any clues as to why the seeding works only when I used plural for the table's name?

This is my migration file after the table is revised from blog to blogs:

class CreateBlogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('title', 255);
            $table->text('body');
            $table->text('user_id');
        });
    }

This is my factory - named BlogFactory:

namespace Database\Factories;

use App\Models\Blog;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class BlogFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Blog::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'title' => $this->faker->sentence, //Generates a fake sentence
            'body' => $this->faker->paragraph(30), //generates fake 30 paragraphs
            'user_id' => User::factory() //Generates a User from factory and extracts id

        ];
    }
}

This is my model - named Blog:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Blog extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'body',
        'user_id',
    ];
}

This is my controller - named BlogController:

<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return Blog::all();
    }
Beaumont
  • 313
  • 5
  • 16
  • 1
    It is very hard to help you without code - did you put `protected $table = 'blog';` in the model? Laravel will per default pluralize the model for its table name, since you decided to change that you need to inform Laravel you did this. If not you most likely made a typo in your seeders. Also, the error says no relations found for blogs. Did you intend for a blogs relationship with maybe a user? Lots of things could be wrong here. The best advice is to stick to Laravel defaults until you get more experience. It is hard to fight the framework and learn at the same time – Nicklas Kevin Frank Aug 30 '21 at 09:29
  • Hi @NicklasKevinFrank, I have added the code for reference. These codes are the only reference I have made in regard to "blog". I have only called the data from the routes ```api.php``` which leads back to the controller - for example ```Route::get('/blogs', [BlogController::class, 'index']);``` – Beaumont Aug 30 '21 at 09:44
  • Wait... are you changing the migration of an already running application? – Nicklas Kevin Frank Aug 30 '21 at 09:48
  • I haven't changed it. I just started the project and I was setting up the migration. My intention was for the blog table to be singular named and then proceed to seed it. But when I seed it, the terminal just screams "no relation found for blogs". So the only way for me to work around the problem was to use "blogs" in plural form. FYI - I didn't "php artisan serve" yet. I was just doing seeding. – Beaumont Aug 30 '21 at 09:50
  • Great - I was just making sure you weren't editing old migrations. Once a migration has been run in production, it is generally considered bad practice to edit them. You should rather make a new migration and make the changes there. But in this case, you are still developing, so everything is fine :) – Nicklas Kevin Frank Aug 30 '21 at 09:54
  • 2
    Does this answer your question? [Laravel seed issue, laravel is looking for plural table name](https://stackoverflow.com/questions/49351866/laravel-seed-issue-laravel-is-looking-for-plural-table-name). How hard can it be to read the [documentation](https://laravel.com/docs/8.x/eloquent#table-names) before doing anything ? Are you trying to learn the framework by not reading it and guessing everything ? Also this question was already asked... how hard can it be to read the documentation and search for stuff... I am tired of this questions... – matiaslauriti Aug 30 '21 at 10:01
  • @matiaslauriti No disrespect, I read that page first thing 2 weeks ago when I picked up laravel right after reviewing php syntax. The "plural vs singular" was not immediately apparent to me as I have just finished a 4 months bootcamp after a career switch. So forgive me if I am a bit slow in logic or experience but if it wasn't for Nicklas, I wouldn't even have made the connection to "PvsS", nor the connection to that part of the doc now that you have flagged it up. Granted I am a novice but I AM learning and by no means lazy - which seems to be what you are inferring. – Beaumont Aug 30 '21 at 10:16
  • @Beaumont pardon me, I see a lot of this questions, and as you can see I get really angry. People are lazy, we are developers, the most important thing to be a successful developer is knowing how to search for anything you need... if you don't know how to do that and don't care about getting better with it, then you are going to fail really big (I am generally speaking, not specifically you). – matiaslauriti Aug 30 '21 at 10:19
  • @Beaumont had a problem, now he no longer has that. Looks like he has a great future as a good developer and problem solver. – Nicklas Kevin Frank Aug 30 '21 at 10:24
  • @all - I appreciate this. No harm done, truly. And thank you. – Beaumont Aug 30 '21 at 10:25

2 Answers2

1

I seem to recall similar issues regarding renamed class-names and/or tables... Could you try running "composer dump-autoload" after having all the migrations and/or class-name refactorings ready? I remember old class-names being somehow cached, which gave me an headache. Apparently dumping autoload should rebuild this cache.

Janne
  • 1,111
  • 3
  • 12
  • 17
0

Edit: With your newest edit you will not need to do this change as you have changed the migration from creating a blog table to creating a blogs table.

You will need to edit your model to understand that you use non-standard Laravel formatting for your table name.

Laravel, per default, will assume your table is a pluralized version of your model name. To override this, you need to add the following attribute to the model: protected $table = 'blog';

See below code; however, I suggest not changing this and sticking to the Laravel defined standards. The closer you are to doing it the way the framework wants, the easier it will be for you. Especially when learning, it is much easier not to fight the framework at the same time.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Blog extends Model
{
    use HasFactory;

    protected $table = 'blog';

    protected $fillable = [
        'title',
        'body',
        'user_id',
    ];
}
Nicklas Kevin Frank
  • 6,079
  • 4
  • 38
  • 63
  • Great! Thank you. But may I ask what's the logic behind pluralising per default? Now that I know laravel pluralises it, I need to understand "why". – Beaumont Aug 30 '21 at 09:49
  • Beaumont - it's just a sensible standard. Laravel tries to help you follow as many good practices per default. It is a choice, one that Laravel decided to solve this way, you can find many articles discussing the benefits of either way if you google, "database plural vs singular" a bunch should come up. – Nicklas Kevin Frank Aug 30 '21 at 09:52
  • I also had doubts regarding plural DB-naming, but having gotten used to Laravels naming conventions it has started to make sense. Blog is an instance of something which is stored within "blogs". Sometimes this could still lead to a issue with certain words which have unconventional singular vs plural pattern, something similar to such as "man" vs "men". For this the way is to go as answered by @NicklasKevinFrank – Janne Aug 30 '21 at 10:03