convert mysql query to laravel builder 4.2 - laravel-4

SELECT user_id, email, username, password, COUNT(*) AS count
FROM users
where email = 'dyz#dss.com'
GROUP BY password HAVING count > 0
ORDER BY count DESC;
I tried with below code does not work Please guide how to do this thanks in advance
User::SELECT('user_id, email, username, password, COUNT(*) AS count')
->where('email', Input::get('username'))
->groupBy('password')
->havingRaw('count > 0')
->orderBy('count', 'DESC')
->get();

You should try this
DB::table('users')
->select(DB::raw('count(*) as count, user_id, email, username, password'))
->where('email', Input::get('username'))
->orderBy('count', 'DESC')
->groupBy('password')
->having('count', '>', 0)
->get();

Related

Sort results where "seen" matches 2

I have a table for stories with fields id, user_id, file_name, seen (1-2)
Stories: id, user_id, file_name, seen
I would like to create a list that takes all the stories but with a primary order where seen corresponds to 2 (still to be seen then)
I have this simple query as a test, what should I do?
$users = App\Models\User::whereHas('stories', function ($query) {
})->get();
You can try next:
$users = App\Models\User::whereHas('stories')->with(['stories' => function($query) {
$query->orderBy(DB::raw('stories.seen=2'), 'desc')
}])->get();
Or
$users = App\Models\User::whereHas('stories')
->select('*')
->join('stories', 'stories.user_id', '=', 'users.id')
->orderBy(DB::raw('stories.seen=2'), 'desc')
->get();

How to only get data with today's date in laravel

I'm trying to display only data that has today's date in the created_at timestamp.
But I'm getting all my data instead of only today's data
Here is my code
$now = now();
$products = Product::selectRaw('status, created_at, date(created_at), hour(created_at), count(*) as total')
->whereRaw('created_at', $now)
->groupByRaw('status, date(created_at), hour(created_at), created_at')
->get();
$now = now();
$products = Product::selectRaw('status, created_at, date(created_at), hour(created_at), count(*) as total')
->whereDate('created_at', '=', date('Y-m-d'))
->groupByRaw('status, date(created_at), hour(created_at), created_at')
->get();
The following code should work.
$products = Product::selectRaw('status, created_at, date(created_at), hour(created_at), count(*) as total')
->whereDate('created_at', DB::raw('CURDATE()'))
->groupByRaw('status, date(created_at), hour(created_at), created_at')
->get();

Laravel Parameter Grouping (and/or where)

I upgraded Laravel to version 7, and when I do a query like this:
$users = User::where('name', '=', 'John')
->where(function ($query) {
$query->where('votes', '>', 100)
->orWhereNull('title');
})
->get();
it doesn't work as expected, and I got this error [SQL Server] Must specify table to select from
because the SQL should be like this:
select * from users where name = 'John' and (votes > 100 or title is null)
but when I debug the returned query it shows like this:
select * from users where name = 'John' and (select * votes > 100 or title is null) is null
The above query it just an example of my complex query, and I have a lot like this query in all of my project so I don't need a replacement, I just need to know how to fix it as it worked fine before upgrading
You can use whereRaw for the alternative method, for example
$users = Table::whereRaw(" name=? AND (votes=? OR title=?)", array(?,?,?))
$users = User::where('name', '=', 'John')
->where(function ($query) {
$query->from('users')
->where('votes', '>', 100)
->orWhereNull('title');
})
->get();

Query builder in Laravel in not working

I have sql query that is working fine in MySQL. I want to get profile id from profiles table using join with user table.
SELECT profiles.profile_id FROM profiles
left JOIN users on profiles.id = users.id where users.id = 1
Laravel Query builder query is
$my_user_id = Auth::user()->id;
$my_id = DB::table('profiles')->select('profiles.profile_id')
->leftjoin('users','users.id' ,'=' ,'profiles.id')
->pluck('profiles.profile_id')
->where('users.id',$my_user_id)
->first();
Call where() before pluck() (select() is not necessary):
$my_id = DB::table('profiles')
->leftJoin('users', 'users.id', '=' , 'profiles.id')
->where('users.id', $my_user_id)
->pluck('profiles.profile_id')
->first();
You can also shorten it with value():
$my_id = DB::table('profiles')
->leftJoin('users', 'users.id', '=' , 'profiles.id')
->where('users.id', $my_user_id)
->value('profiles.profile_id');
You should select the id as well to perform the join :
DB::table('profiles')->select('profiles.profile_id', 'profiles.id')
->leftjoin('users','users.id' ,'=' ,'profiles.id')
->where('users.id',$my_user_id)
->pluck('profiles.profile_id')
->first();

joining 3 tables in laravel using DB with similar column names

I have three 3 tables in my database user_projectswith columns
project_id,
user_id,
role
project_invitations with columns
project_id,
email,
role
and finally users
id,
email
I want to join the three tables using the laravel query builder so that I can get
userid, role, email, invited_user
null member test#gmail.com true
1 owner test123#gmail.com false
2 member test12#gmail.com true
So far I got the following data
[
{
"email":"test#sharklasers.com",
"id":2,
"role":"owner",
"invitedEmail":"test#gmail.com",
"invitedRole":"member"
},
{
"email":"test#sharklasers.com",
"id":2,
"role":"owner",
"invitedEmail":"safda#gmail.com",
"invitedRole":"member"
}
]
using the following query
DB::table('user_projects')
->join('users', 'user_projects.user_id', '=', 'users.id')
->join('project_invitations', 'project_invitations.project_id','=','user_projects.project_id')
->where('user_projects.project_id', '=', $projectId)
->select('users.email', 'users.id', 'user_projects.role', 'project_invitations.email as invitedEmail','project_invitations.role as invitedRole')
->get();
You need to use outer joins to get all records even if there is not a join for the record.
DB::table('user_projects')
->join('users', 'user_projects.user_id', '=', 'users.id', 'outer')
->join('project_invitations', 'project_invitations.project_id','=','user_projects.project_id', 'outer')
->where('user_projects.project_id', '=', $projectId)
->select('users.email', 'users.id', 'user_projects.role', 'project_invitations.email as invitedEmail','project_invitations.role as invitedRole')
->get();

Resources