Home>

Development environment
vscode
mac os
laravel 8
Virtual environment with mamp

HereWhenHereI was creating a database based on this site, but it seems that the model and controller cannot be connected.

First of all, for some reason, the folder that contains the seeder files
database>seeders>UserEntrySeeder.php
It seems that it is different from the sample.

I also created a seeder on the above site.

$php artisan make: seeder UserEntrySeeder

UserEntrySeeder.php

<? php
namespace Database \ Seeders;
use Illuminate \ Database \ Seeder;
class UserEntrySeeder extends Seeder
{
   /**
     * Run the database seeds.
     *
     * @return void
     * /
    public function run ()
    {
        DB :: table ('user_entry')->insert ([
            'title' =>"Dummy title",
            'body' =>"Dummy body",
        ]);
    }
}


There is an error in the DB here, and there is a wavy line error in vscode and an error with a red circle in the file.

UserEntryController.php

<? php
namespace App \ Http \ Controllers;
use Illuminate \ Http \ Request;
use App \ models \ UserEntry;
class UserEntryController extends Controller
{
    function index () {
        $all = UserEntry :: all ();
        dd ($all);
    }
}


Model UserEntry.php

<? php
namespace App \ Models;
use Illuminate \ Database \ Eloquent \ Factories \ HasFactory;
use Illuminate \ Database \ Eloquent \ Model;
class UserEntry extends Model
{
    use HasFactory;
    protected $table = "user_entry";
}

What is displayed on the site

Illuminate \ Database \ Eloquent \ Collection {# 307 ▼
  #items: []
}

Please teach!

  • Answer # 1

    The seeder has changed so much in Laravel 8 that the old information is useless.

    This time, UserEntrySeeder.php may not have enough DB.

    use Illuminate \ Support \ Facades \ DB;

    It is a mistake to see such a strange blog.
    Laravel only needs to see GitHub, the official documentation and its Japanese translation.

  • Answer # 2

    Since there is little information on laravel8 itself, I decided to develop it with 7.
    I'm sorry for the question that is not helpful to other people.

Related articles