Laravel сводная таблица отношений

Сегодня я собираюсь перечислить новую проблему, касающуюся отношений между людьми.
Есть три таблицы:

АЛЬБОМНЫЙ СТОЛ

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

ALBUM_PHOTO (PIVOT TABLE)

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

Как я могу решить это?
Спасибо

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector