简体   繁体   English

如何用Laravel术语编写PHP SQL查询

[英]How to write PHP SQL query in laravel terms

How would I write this php mysql query in laravel's terms. 我将如何用laravel的术语编写此php mysql查询。 I've checked the documentation and cannot find an IF or AS in there documentation for SQL in Laravel 4.2 我已经检查了文档,但在Laravel 4.2中找不到有关SQL的IF或AS

This is the php query: 这是php查询:

query("SELECT IF(friends.sender = ".$_SESSION["user"].", friends.recipient, friends.sender) AS user_id FROM friends WHERE friends.status=1 AND friends.sender = ".$_SESSION["user"]." OR friends.recipient = ".$_SESSION["user"]." AND friends.status=1 ")

I'm trying to format it like this, 我正在尝试像这样格式化

@foreach(Friends::if("sender","=",Auth::user()->id)->as("user_id")->where("status","=",1)->where("sender","=",Auth::user()->id)->orWhere("recipient","=",Auth::user()->id)->where()->get() as $user_friends)

You can use Query builder or Eloquent to do that 您可以使用查询构建器或Eloquent来做到这一点

With Eloquent (this code will be added to the controller) 与雄辩(此代码将被添加到控制器)

public function getFriends($arg1, $arg2)
{
  $friends = Frind::where('filed-name', 'operator', $arg1)->where('filed-name', 'operator', $arg2)->all();
  return view('/view-name', ['friends' => $friends];
}

or with Query Builder 或使用查询生成器

public function getFriends($arg1, $arg2)
{
  $friends = DB::table('friends')->select('friends.*')->where('filed-name', 'operator', $arg1)->where('filed-name', 'operator', $arg2)->get();
  return view('/view-name', ['friends' => $friends];
}

You can read more on Query builder here and Eloquent here . 您可以在此处和Eloquent上阅读有关查询构建器的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM