在数据库中存储外键值(laravel)

时间:2016-04-26 20:00:44

标签: laravel laravel-5

我是laravel的新手。我很难将外键值存储在数据库中。我已阅读文档,在stackoverflow上搜索。但它让我更加困惑。

我有一个User表和一个idea表。用户可以创造很多想法。让我展示一下代码:

控制器功能:

 public function storePost(IdeaRequest $request) {
        Idea::create([
            'title' => $request->input('idea_title'),
            'user_id' => $request->hasMany('user_id'),
            'image' => $request->file('idea_image')->move('images'),
            'info' => $request->input('idea_info'),
            'communitySelection' => $request->input('selection'),
            'location' => $request->input('idea_location'),
            'goal' => $request->input('idea_goal'),
            'description' => $request->input('idea_description')
        ]);

    }

理念模型:

class Idea extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'idea_title', 'idea_info','user_id', 'idea_image', 'idea_description', 'duration', 'idea_goal', 'pledge_amount', 'set_equity',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'remember_token',
    ];

    public function Post() {
        return $this->hasMany('User');
    }
}

想法迁移:

  public function up()
    {
        //
        Schema::create('ideas', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('idea_title');
            $table->string('idea_info', 150);
            $table->string('idea_image');
            $table->string('selection');
            $table->longText('idea_description');
            $table->string('idea_location');
            $table->integer('idea_goal')->unsigned();
            $table->integer('pledge_amount')->unsigned();
            $table->string('set_equity')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }

用户迁移:

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',50);
            $table->string('email')->unique();
            $table->string('password',32);
            $table->string('profile_picture')->nullable();
            $table->string('address');
            $table->string('phone_number',15);
            $table->boolean('status')->nullable();
            $table->string('user_type',15);
            $table->string('credit_card',16)->unique();
            $table->rememberToken();
            $table->timestamps();
        });
    }

可以看出正在设置外键。我无法将外键的值存储在用户表中。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

Idea Migration 中,您可以像这样声明外键

public function up()
    {
        //
        Schema::create('ideas', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('idea_title');
            $table->string('idea_info', 150);
            $table->string('idea_image');
            $table->string('selection');
            $table->longText('idea_description');
            $table->string('idea_location');
            $table->integer('idea_goal')->unsigned();
            $table->integer('pledge_amount')->unsigned();
            $table->string('set_equity')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });

        //Declare foreign key
        Schema::table('ideas', function(Blueprint $table) {
            $table->foreign('user_id')->references('id')->on('users')
                        ->onDelete('restrict')
                        ->onUpdate('restrict');
        });
    }

我不同意您在构思模型中添加的关系。您应该使用belongsTo代替hasMany。根据评论,将Post()更改为用户()

public function User() {
    return $this->belongsTo('User');
}

如果是,您还需要更改控制器功能中的'user_id' => $request->hasMany('user_id')