Laravel study notes (16) Blog writing function (data migration, relationship model, data padding)

  1. Additional blogs table

Generate migrations file

// 表名blogs 文件名 create_blogs_table
php artisan make:migration --create=blogs create_blogs_table

Additional content

    public function up()
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->string('content', 200);
            $table->integer('user_id')->index();
        });
    }

executable file

// 生成新追加的表
artisan migrate
// 或者表全部重新生成
artisan migrate:fresh --seed
  1. Blog generation model and resource controller BlogController and routing
  2. View file generation
	<div class="form-group">
	    <label for="">内容</label>
	    <textarea class="form-control" name="content">{{old('content')}}</textarea>
	</div>
  1. Blog models
	// 添加白名单
    protected $fillable = ['content', 'user_id'];

$ Fillable only batch control attributes assigned whitelist permitted to be filled, Store () method does not assign a batch uncontrolled.
$ fillable with $ guarded only limits the create method, without limiting the save method.

$ Fillable and $ guarded two attributes are used to control the bulk assignments, bulk assignments What does it mean? It is not populated with data, once filled to the concept of a database of N records, here (talking about $ fillable and $ guarded) so-called bulk assignment refers only to property value when creating a record of incoming. The so-called bulk quantities with respect to the N requests for.

  1. User model an additional way to make it is associated with a Blog
    public function blogs() {
        return $this->hasMany(Blog::class);
    }

laravel belongsToMany hasMany usage and resolve
inter-relationship model (association) hasMany, belongsTo usage

  1. Controller calls
    \Auth::user()->blogs()->create($data);

\ Auth :: user () returns an instance of the User model, then call the method in which the blogs, and finally create the data
because the association model, it will automatically be automatically given the User id user_id Blog, if not called user_id is not automatically associated need hasMany specified parameters

  1. Blog generation model factory and tested under tinker
artisan make:factory --model=Blog BlogFactory // 必须要--model,不然找不到模型
$factory->define(Blog::class, function (Faker $faker) {
    return [
        'content' => $faker->text(20),
        'user_id' => $faker->randomElement([1, 2, 3]) // 随机取123中的一个数
    ];
});
factory(App\Blog::class, 20)->make()
  1. Generates a fill file
    public function run()
    {
        factory(\App\Blog::class, 20)->create();
    }

Registration documents

public function run()
{
    // $this->call(UsersTableSeeder::class);
    $this->call(UserSeeder::class);
    $this->call(BlogSeeder::class);
}
  1. carried out
Published 40 original articles · won praise 0 · Views 776

Guess you like

Origin blog.csdn.net/qj4865/article/details/104214376