小编Ali*_*aza的帖子

Laravel 5用外键插入行

我有两个表用户和帖子.这是我的用户表迁移文件:

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('password_temp',60);
        $table->integer('active');
        $table->string('code',60);
        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('users');
}
Run Code Online (Sandbox Code Playgroud)

这是我的帖子表迁移文件

public function up()
{
    Schema::create('posts', function(Blueprint $table){

        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->integer('user_id')->unsigned();
        $table->string('slug');
        $table->timestamps();


    });

    Schema::table('posts',function(Blueprint $table){

        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade')
            ->onUpdate('cascade');

    });
}
Run Code Online (Sandbox Code Playgroud)

AdminPostsController扩展Controller {public function store(Request $ request){

    $validator = Validator::make($request->all(),Post::$rules);


    if($validator->passes()){



        $post = new Post();

        $post->title = $request->get('title');
        $post->body = …
Run Code Online (Sandbox Code Playgroud)

php mysql laravel-5

5
推荐指数
1
解决办法
9828
查看次数

标签 统计

laravel-5 ×1

mysql ×1

php ×1