【问题标题】:getAttribute method is not calling laravel using query buildergetAttribute 方法未使用查询生成器调用 laravel
【发布时间】:2021-06-01 09:30:10
【问题描述】:

我尝试了多种方法在模型中调用下面的方法

public function getFullNameAttribute()
    {
        return "{$this->first_name} {$this->last_name}";
    }

我尝试了什么:

User::select('first_name', 'last_name')
        ->with([
            'fullName' => function($query){
                $query->select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"));
            }
        ])
        ->where('user_id', $id)
        ->where('is_deleted', 0)
        ->first();

我尝试的另一种方法:

User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"))
        ->with([
            'fullName' => function($query){
                $query->select('firstname', 'lastname');
            }
        ])
        ->where('user_id', $id)
        ->where('is_deleted', 0)
        ->first();

但没有任何东西在调用 get<>Attribute 名称。

【问题讨论】:

  • 为什么要混用下划线? firstnamefirst_name - 是哪个?

标签: php laravel laravel-8 accessor


【解决方案1】:

访问器不是关系。试试

$user = User::where('user_id', $id)->first();


dd($user->full_name);

【讨论】:

    【解决方案2】:

    这些是 Laravel 中的神奇方法。
    这意味着这些将按以下方式处理:

    1. getFullNameAttribute 被剥离 getattribute;留下FullName
    2. 然后这将被转换为snake_case,从而产生full_name
    3. 然后将此值添加到 attributes 数组中,该数组可在视图中访问,就像常规属性一样。

    实际上,这将为您留下以下代码。

    $user = User::first();
    echo $user->full_name;
    

    这假设您采用了以下第一种方法:

    public function getFullNameAttribute()
    {
        return $this->first_name . ' ' . $this->last_name;
    }
    

    如果您想以$user->fullname 的身份访问该属性,那么您需要采用以下方法:

    public function getFullnameAttribute()
    {
        return $this->first_name . ' ' . $this->last_name;
    }
    

    请注意,我们现在有 Fullname,带有小写的 n
    所有魔术 getter(和 setter)都是这种情况,它们将根据 PascalCase 转换为 snake_case

    有趣的是,您可以执行以下操作并且它会起作用:

    getfull_nameAttribute()
    {
        // ...
    }
    

    Relevant framework code.
    Laravel 关心的是 getAttribute 书挡。

    【讨论】:

      【解决方案3】:

      在模型中定义方法

      public function getFullNameAttribute()
          {
              return "{$this->first_name} {$this->last_name}";
          }
      

      然后在查询中,例如,如果您正在获取一条记录,那么

       $res= \App\Models\User::first();
      
         dd($res->fullName);
      

      这意味着你的属性是FullName,那么第一个字母在访问时变成small letter

      【讨论】:

        【解决方案4】:

        当你尝试时

        public function getFullNameAttribute()
        {
           return "{$this->first_name} {$this->last_name}";
        }
        

        然后在模型内部你也需要添加它

        protected $appends = ['full_name'];
        

        这是你错过的然后它会起作用 参考链接https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

        【讨论】:

        • 如果你要去serialize模型,你只需要做$appends部分。
        【解决方案5】:

        这不是accessors 的工作原理。您通过使用with() 将其视为关系。只需从查询中删除with(),查询用户后,您就可以访问$user->full_name;

        【讨论】:

          猜你喜欢
          • 2017-07-21
          • 2016-05-13
          • 2014-04-10
          • 2018-07-03
          • 2015-08-06
          • 2013-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多