代码之家  ›  专栏  ›  技术社区  ›  3x071c

用户模型上不存在最新的拉氏叶片法

  •  0
  • 3x071c  · 技术社区  · 7 年前

    在我的用户模型中( App\User.php

    public function actionables() {
      return $this->hasMany(Actionable::class, 'owner_id', 'id');
    }
    

    在actionables表的迁移方案中,我指定了以下外键:

    $table->foreign('owner_id')->references('id')->on('users')->onDelete('cascade');
    

    当我打电话

    {{ auth()->user()->actionables->where('act_type', 'order')->latest()->content }}
    

    Method Illuminate\Database\Eloquent\Collection::latest does not exist. 
    

    我试过用 ->get() 使用其他方式访问用户模型( Auth::user )但是到目前为止还没有解决这个问题。

    4 回复  |  直到 7 年前
        1
  •  4
  •   jfadich    7 年前

    auth()->user()->actionables 
    

    全部的 作为一个集合的结果。

    SELECT * FROM actionables WHERE owner_id = ?
    

    where 方法,其功能类似于查询生成器方法,但必须认识到,它是在PHP而不是SQL中执行搜索,因为查询已经执行并且不能再更改。 latest() 存在于查询生成器上,但不存在作为错误来源的集合。

    user()->actionables()

    auth()->user()->actionables()->where('act_type', 'order')->latest()->first() 
    

    SELECT * FROM actionables WHERE act_type = order AND WHERE owner_id = ? ORDER BY created_at DESC LIMIT 1
    
        2
  •  1
  •   Thamer    7 年前

    latest Collection 方法。

    *注意你应该 created_at Actionable 桌子。

    auth()->user()->actionables()->where('act_type', 'order')->orderBy('created_at', 'desc')->first()->content
    
        3
  •  1
  •   dparoli    7 年前

    您应该使用查询生成器 actionables() 不是收藏品 actionables

    {{ auth()->user()->actionables()->where('act_type', 'order')->latest()->first()->content }}
    
        4
  •  0
  •   Davit Zeynalyan    7 年前

    一定是 ->last() ->latest()

    {{ auth()->user()->actionables->where('act_type', 'order')->last()->content ?? ""}}