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

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

如何在 Laravel Eloquent 中調用 DB 函數并使用分頁?

如何在 Laravel Eloquent 中調用 DB 函數并使用分頁?

PHP
陪伴而非守候 2024-01-19 16:53:48
我正在嘗試將 Laravel 5.8 Eloquent 與自定義函數和原始 sql 一起使用我有一張桌子Question(id  integertitle stringdescription text)以及控制器中的此功能public function index(){    // go to the model and get a group of records    //$question = Question::orderBy('id', 'desc')->paginate(3);    $question = Question::selectRaw(DB::raw('id,title,SUBSTRING(description,0,10) as description'))        ->orderByDesc('id')        ->paginate(3);    return view('questions.index')->with('questions', $question);}我的目標是將分頁與數據庫函數一起使用,如上面的示例。查詢有效,分頁有效,返回 3 列id,title但description描述為empty。組裝查詢的正確方法是什么?下面是轉儲[original:protected] => Array ( [id] => 20 [title] => text [description] => )
查看完整描述

1 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

你不需要selectRaw(DB::raw('')),只需selectRaw('')

public?function?index(){
????$question?=?Question::selectRaw('id,?title,?SUBSTRING(description,?1,?10)?AS?description'))
????????->orderByDesc('id')
????????->paginate(3);
????????????return?view('questions.index')->with('questions',?$question);
}

更新:

我剛剛查看 MySql 文檔,看到了這個:

?A?value?of?0?for?pos?returns?an?empty?string.

你應該做這個:

SUBSTRING(description,?1,?10)


這里的問題與 Laravel 無關,是對 MySql 用法的誤解。

我想這就能解決空的問題了!


查看完整回答
反對 回復 2024-01-19
  • 1 回答
  • 0 關注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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