簡體   English   中英

選擇和Laravel雄辯的地方

[英]Select and where clausule on Laravel eloquent

我試圖獲取數據庫的三列,但我不明白...

我可以使用此代碼。 但是這種方式返回了所有列,只有我想要其中三個。

User::where('name', 'LIKE', $term)->get()

我知道使用其他代碼:但是...在其中,我不能使用“ where”子句。

User::select(array('id', 'name', 'email'))

有人知道將兩種代碼合並的一種方法嗎? 也許唯一的方法就是使用Fluent?

因此,將它們鏈接在一起。 Eloquent允許您將任意數量的語句添加在一起,直到調用閉包之一get()first()為止:

$users = User::select(array('id', 'name', 'email'))->where('name', 'LIKE', '%'.$term.'%')->get();

調用完上述內容后,您可以調用foreach以顯示所有結果:

foreach($users AS $user){
  echo $user->id;
  echo $user->name;
  echo $user->email;
}

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM