简体   繁体   English

Laravel Eloquent多对多附加

[英]Laravel Eloquent many-to-many attach

I am new to laravel, and trying to build a photo album with it. 我是laravel的新手,并试图用它制作相册。 My problem is that i use the attach function to insert the user id and group id to my database, it works okay, but in the documentation it says this about the attach function 我的问题是我使用attach函数将用户ID和组ID插入我的数据库,它工作正常,但在文档中它说明了关于attach函数

For example, perhaps the role you wish to attach to the user already exists. 例如,您希望附加到用户的角色可能已存在。 Just use the attach method: 只需使用attach方法:

So i wanted to use it the same way, if the album_id already exist just update it, other wise insert thr new one, but my problem is it always insters, it does not checks if the album_id already exsits 所以我想以同样的方式使用它,如果album_id已经存在只是更新它,其他明智的插入新的一个,但我的问题是它总是insters,它不检查album_id已经存在

My model 我的模特

class User extends Eloquent 
{
    public static $timestamps = false;

    public function album()
    {
        return $this->has_many_and_belongs_to('album', 'users_album');
    }

}

Post function 发布功能

public function post_albums()
    {

      $user = User::find($this->id);

      $album_id = Input::get('album');

      $path = 'addons/uploads/albums/'.$this->id.'/'. $album_id . '/';

      $folders = array('path' => $path, 'small' => $path. 'small/', 'medium' => $path. 'medium/',  );

      if (! is_dir($path) ) 
      {
         foreach ($folders as $folder) 
         {
            @mkdir($folder, 0, true);
         }
      }


     $sizes = array( 
        array(50 , 50 , 'crop', $folders['small'], 90 ), 
        array(164 , 200 , 'crop', $folders['medium'], 90 ), 
     );

     $upload = Multup::open('photos', 'image|max:3000|mimes:jpg,gif,png',  $path)
             ->sizes( $sizes )
             ->upload();

     if($upload) 
     {
        $user->album()->attach($album_id);
        return Redirect::back();
     } 
     else 
     {
        // error show message remove folder
     }   

   }

Could please someone point out what im doing wrong? 可以请有人指出我做错了什么? Or i totally misunderstod the attach function? 或者我完全误解了附加功能?

I believe you have misunderstood the attach function. 我相信你误解了附加功能。 The sync function uses attach to add relationships but only if the relationship doesn't already exist. 同步功能使用附加来添加关系,但前提是该关系尚不存在。 Following what was done there, i'd suggest pulling a list of id's then only inserting if it doesn't already exist in the list. 按照那里所做的,我建议拉一个id列表,然后只插入列表中尚不存在的内容。

$current = $user->album()->lists( 'album_id' );
if ( !in_array( $album_id, $current ) )
{
    $user->album()->attach( $album_id );
}

On a side note I'm going to suggest that you follow the default naming convention from laravel. 另外,我建议您遵循laravel的默认命名约定。 The relationship method should be $user->albums() because there are many of them. 关系方法应该是$ user-> albums(),因为它们中有很多。 The pivot table should also be named 'album_user'. 数据透视表也应命名为“album_user”。 You will thank yourself later. 你以后会感谢自己。

Contains method of Laravel Collections 包含Laravel Collections的方法

The laravel collections provides a very useful method 'contains'. laravel集合提供了一种非常有用的方法'包含'。 It determine if a key exists in the collection. 它确定集合中是否存在密钥。 You can get the collection in your case using $user->album . 你可以使用$user->album获得你的收藏。 You can note the difference that album is without paranthesis. 你可以注意到专辑没有副作用的区别。

Working code 工作代码

Now all you had to do is use the contains method. 现在你所要做的就是使用contains方法。 The full code will be. 完整的代码将是。

if (!$user->album->contains($album_id)
{
   $user->album()->attach($album_id);
}

Its more cleaner and 'Laravel' way of getting the required solution. 它更清洁和'Laravel'获得所需解决方案的方式。

Thanks @Collin i noticed i misunderstand i made my check yesterday 谢谢@Collin我注意到我误解了我昨天做了我的支票

$album = $user->album()->where_album_id($album_id)->get();

        if(empty($album))
        {
           $user->album()->attach($album_id);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM