列名称如column_a_name或coumn_10

时间:2015-12-16 10:30:13

标签: php laravel eloquent accessor

我正在使用Laravel访问器,除了其中两个外,它们都在工作:

public function getLambdaDCheckboxAttribute($value)
{
    ...
}

其中DB列的名称是lambda_d_checkbox和

public function getUber40CheckboxAttribute($value)
{
   ...
}

其中DB列名称为uber_40_checkbox。

我猜第一个例子中的问题是两个下划线之间的“d”字母和第二个例子中的数字“40”。如何完成对这些访问者的调用?

我有以下结构:

class UploadIconsRES extends Eloquent{
   private $verbesserte_text = "";
   private $verbesserte_image = "";
   private $lambda_d_text = "";
   private $lambda_d_image = "";

   public function getVerbesserteCheckboxAttribute($value)
   {
      if($value == "yes")
      {
          $this->verbesserte_image= 'http://h02b.ebau.at/austrotherm/1280/'.DB::table('upload_icons_url')->where('id', 1)->pluck('icon_url');
          $this->verbesserte_text = DB::table('upload_icons_text')->where('id', 1)->pluck('icon_name');
      }

      return $value;
   }

   public function getLambdaDCheckboxAttribute()
   {        
      if($this->lambda_d_checkbox == "yes")
      {
          $this->lambda_d_image ='http://h02b.ebau.at/austrotherm/1280/'.DB::table('upload_icons_url')->where('id', 34)->pluck('icon_url');
          $this->lambda_d_text = DB::table('upload_icons_text')->where('id', 34)->pluck('icon_name');
      }

      return $value;
  }

  public function toArray()
  {
     $array = parent::toArray();
     $array['icons'] =  array();

     if($this->verbesserte_image != "")
     {
        $icon =  array();
        $icon['url'] = $this->verbesserte_image;
        $icon['text'] = $this->verbesserte_text;

        array_push($array['icons'],  $icon);
     }  

     if($this->lambda_d_image != "")
     {
        $icon =  array();
        $icon['url'] = $this->lambda_d_image;
        $icon['text'] = $this->lambda_d_text;

        array_push($array['icons'],  $icon);
     }

     return $array;
  }

...然后

class ProductMetaRES  extends Eloquent {

   protected $table = 'products_meta_res';
   protected $primaryKey = 'product_id';

   public function UploadIconsRES()
   {
       return $this->hasMany("UploadIconsRES", "productID", "id");
   }
}

...然后

 class ProductLangRes extends Eloquent
 {
     protected $table = 'products_lang_res';

     public function ProductMetaRES()
     {
       return $this->hasOne("ProductMetaRES", "product_lang_FS");
     }
 }

最后当我打电话时,在我的控制器中使用这些类:

$content = ProductLangRES::with( 'ProductMetaRES.UploadIconsRES')->get(array('id', 'de_AT as name', 'de_AT_description as description'));

我没有得到所需的icon_url和icon_text。另外我在那些工作正常的类中有更多的访问器,但为了简单起见,我没有在这里的例子中包含它们。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

public function getLambdaDCheckboxAttribute()
{
    return $this->lambda_d_checkbox;
}

public function getUber40CheckboxAttribute()
{
    return $this->uber_40_checkbox;
}