Php Laravel 5添加多对多关系
我在将多个一对多关系值存储到数据库时遇到问题 迁移:Php Laravel 5添加多对多关系,php,laravel,one-to-many,laravel-5,Php,Laravel,One To Many,Laravel 5,我在将多个一对多关系值存储到数据库时遇到问题 迁移: Schema::create('articles', function(Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->string('title'); $table->string('slug', 10
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('slug', 100)->unique();
$table->LongText('body');
$table->text('excerp');
$table->integer('img_id')->unsigned()->nullable();
$table->integer('img_gal_id')->unsigned()->nullable();
$table->timestamp('published_at');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('img_gal_id')->references('id')->on('image_galleries');
$table->foreign('img_id')->references('id')->on('images');
});
形式
Article.php的一部分
protected $fillable = ['title', 'user_id','slug', 'body', 'excerp','published_at', 'img_id', 'img_gal_id'];
/ **
* Articles from a user.
* One To Many Relasion
* @var array
*/
public function user()
{
return $this->BelongsTo('App\User');
}
/**
* Main Image for article.
* Many to One Relasion
* @var array
*/
public function Image()
{
return $this->BelongsTo('App\Image');
}
形象
控制器
public function store(CreateArticleRequest $request)
{
$images = Input::file('images');
$imgobj = new Image;
// Imgprof = Upload the file and store it to db
$imgobj-> Imgprof($images);
// Uploaded img id :
$Uploadedimgid = $imgobj->Getimgids();
$article = \Auth::user()->articles()->create($request->all());
}
我正在存储2个obj到db图像-imgobj和文章
我已经设法将用户id存储到数据库中,但是现在如何将$uploadedimgid的值存储到相同的$articleto img_id中
我尝试添加image->$imgobj,但没有成功
谢谢 您可能需要执行$article=\Auth::user->articles->create$request->all+['img_id'=>$Uploadedimgid];
/**
*One to many relasionship with article entity
*/
public function articles()
{
return $this->HasMany('App\Article');
}
public function store(CreateArticleRequest $request)
{
$images = Input::file('images');
$imgobj = new Image;
// Imgprof = Upload the file and store it to db
$imgobj-> Imgprof($images);
// Uploaded img id :
$Uploadedimgid = $imgobj->Getimgids();
$article = \Auth::user()->articles()->create($request->all());
}