belykia
320
9
Laravel

assign the value of id in user table to the user_id in posts table automatically ??

Posted 1 year ago by belykia

hello everybody : i want to make the relation between user_id in posts table and id in users table , giving the the user_id the value of id , directly with the logging (one to many) .

in the Post.php model i wrote : public function User(){ return $this->belongsTo('App\User'); } in the User.php model i wrote

public function Post(){ return $this->hasMany('App\Post','user_id', 'user_id'); };

then i have 2 migration tables : the users containt :

Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps();

then the posts containt :

$table->increments('id'); $table->string('asnumber'); $table->string('peeringrs')->nullable(); $table->string('peeringdb')->nullable(); $table->string('asset')->nullable(); $table->string('contact'); $table->integer('user_id'); $table->timestamps();

??

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.