Laravel数据透视表关系

时间:2018-12-28 12:53:24

标签: php mysql laravel pivot-table attachment

今天,我将列出一个有关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表)

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

我该如何解决? 谢谢

0 个答案:

没有答案