I'm working on SQL queries using Eloquent, and I'm having trouble concatenating my SQL strings in my query.
$search = DB::table('players')
->join('nfl_team', 'nfl_team.team_id', '=', 'players.team_id')
->join('colleges', 'colleges.college_id', '=', 'players.college_id')
->join('player_positions', 'nfl_team.team_id', '=', 'players.team_id')
if (isset($first_name) && !empty($first_name))
{
->where('players.FName', 'LIKE', '%'.$first_name.'%')
}
if (isset($last_name) && !empty($last_name))
{
->where('players.LName', 'LIKE', '%'.$last_name.'%')
}
if (isset($team) && !empty($team))
{
->where('players.team_id', '=', $team)
}
if (isset($college) && !empty($college))
{
->where('players.college_id', '=', $college)
}
if (isset($position) && !empty($position))
{
->where('players.position_id', '=', $position)
}
->get();
The problem i'm having is with the code within the if statements.
In PHP, I would use something like this
if ( isset( $_GET['first_name'] ) && !empty( $_GET['first_name'] ) )
{
$sql .= " AND players.FName LIKE '%" . $_GET['first_name'] . "%'";
}
But i'm not sure what the syntax is in Eloquent