亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

拉拉維爾等效于用戶::所有() 函數

拉拉維爾等效于用戶::所有() 函數

PHP
不負相思意 2022-09-12 09:14:57
伙計們我有問題。Laravelapi在我的控制器中,當我使用時,一切都很好,但我需要過濾數據,所以當我使用收集時,它會給我錯誤。錯誤是collection$teacher$teachersCall to undefined method Illuminate\\Database\\Query\\Builder::mapInto();我想要過濾后的數據    $teachers = DB::table('users')->where('role', '=', 1);    $teacher = Users::all();    return UserResource::collection($teachers);我試過這個$teachers = DB::table('users')->where('role', '=', 1)->get();$teachers = DB::table('users')->where('role', '=', 1)->get()->toArray();所以我需要解決方案這是我的資源<?phpnamespace App\Http\Resources;use Illuminate\Http\Resources\Json\JsonResource;class User extends JsonResource{    /**     * Transform the resource into an array.     *     * @param  \Illuminate\Http\Request  $request     * @return array     */    public function toArray($request)    {        return parent::toArray($request);    }}
查看完整描述

3 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

試試這個


$teachers = DB::table('users')->where('role', '=', 1);

$teacher = Users::all();

return new UserResource($teachers);


查看完整回答
反對 回復 2022-09-12
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

我不知道你的,但在我的環境中,這些代碼行工作得很好。嘗試使用命令更新您的拉拉維爾。composer update



查看完整回答
反對 回復 2022-09-12
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

return UserResource::collection(User::where('role', '=', 1)->get());這應該對你有用,你能試試嗎?


更新


您仍然可以將表與 連接,但如果您真的想使用eloquentDB::Table()


然后你必須像這樣指定你的方法。toArray()


    public function toArray($request)

    {

        // return parent::toArray($request);

        return [

            'role' => $this->role,

            'id' => $this->id,

            'name' => $this->name,

            'email' => $this->email,

            'created_at' => $this->created_at,

            'updated_at' => $this->updated_at,

        ];

    }

并會給你結果。return UserResource::collection(DB::table('users')->where('role', '=', 1)->get());


查看完整回答
反對 回復 2022-09-12
  • 3 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號