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

时间:2018-06-17 19:59:15

标签: php sql laravel laravel-4

如何用laravel的术语编写这个php mysql查询。我已检查过文档,但在Laravel 4.2中的SQL文档中找不到IF或AS

这是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 ")

我试图像这样格式化,

@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)

1 个答案:

答案 0 :(得分:1)

您可以使用“查询”构建器或Eloquent执行此操作

使用Eloquent(此代码将添加到控制器中)

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

或使用查询生成器

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];
}

您可以在“查询”构建器here和Eloquent here上详细了解。