mardi 26 avril 2016

Storing foreign key value in database (laravel)

I am new to laravel. i am having difficulties to store foreign key value in database. I have read the documentation, searched on stackoverflow. But its confusing me more.

I have a User table and an idea table. User can create many idea. Let me show the code:

Controller function:

 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')
        ]);

    }

Idea Model:

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');
    }
}

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();
        });
    }

User Migration:

 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();
        });
    }

It can be seen that foreign key is being set. I am unable to store the value of foreign key on a user table. Any help would be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire