简体   繁体   中英

How to write PHP SQL query in laravel terms

How would I write this php mysql query in laravel's terms. I've checked the documentation and cannot find an IF or AS in there documentation for SQL in Laravel 4.2

This is the php query:

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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