今天我要列出一个关于拉斐尔关系的新问题。共有三个表:
相册表
Schema::create('albums', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('image')->default('default.png');
$table->timestamps();
});
照片表
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('image')->default('default.png');
$table->timestamps();
相册照片(透视表)
Schema::create('album_photo', function (Blueprint $table) {
$table->increments('id');
$table->integer('album_id')->unsigned();
$table->integer('photo_id')->unsigned();
$table->timestamps();
$table->foreign('album_id')->references('id')->on('albums');
$table->foreign('photo_id')->references('id')->on('photos');
在这些模型中,我们可以找到以下关系:
专辑
public function photos()
{
return $this->belongsToMany('App\Photo');
}
照片
public function albums()
{
return $this->belongsToMany('App\Album');
}
问题出现在 attach
. 实际上,图像已加载,但与 PivotTable
:
public function imageupload(Request $request)
{
$this->validate($request,[
'name' => 'required',
'image' => 'mimes:jpeg,bmp,png,jpg'
]);
// get form image
$images = $request->file('images');
$slug = str_slug($request->name);
foreach($images as $image){
if (isset($image))
{
// make unique name for image
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
// check album dir is exists
if (!Storage::disk('public')->exists('photo'))
{
Storage::disk('public')->makeDirectory('photo');
}
// resize image for album and upload
$photo = Image::make($image)->resize(1600,479)->stream();
Storage::disk('public')->put('photo/'.$imagename,$photo);
// check album slider dir is exists
if (!Storage::disk('public')->exists('photo/slider'))
{
Storage::disk('public')->makeDirectory('photo/slider');
}
// resize image for album slider and upload
$slider = Image::make($image)->resize(500,333)->stream();
Storage::disk('public')->put('photo/slider/'.$imagename,$slider);
} else {
$imagename = "default.png";
}
$photo = new Photo();
$photo->name = $request->name;
$photo->slug = $slug;
$photo->image = $imagename;
$photo->save();
/***************************************************/
$photo->albums()->attach($request->albums);
/**************************************************/
Toastr::success('Photo Successfully Saved :)' ,'Success');
}
return redirect()->route('admin.album.index');
}
我怎样才能解决这个问题?谢谢
暂无答案!
目前还没有任何答案,快来回答吧!