Getting database results where no records on joined table - php

How do I get results, where there are no results on the joined table, but only for that user?
For example:
$availableQuests = Upload::where('uploads.approved', 1)
->where('uploads.published', 1)
->where('uploads.available', 1)
->where('uploads.type', 'guide')
->join('completed_guides', 'uploads.id', '=', 'completed_guides.upload_id')
->whereNull('completed_guides.id')
->orderBy('published_at', 'desc')
->take(10)
->get();
The problem with this, this accounts for all users, I need to include completed_guides.user_id somehow, to get all the uploads where the id doesnt match in completed_guides
If that makes sense?

Related

How to combine two sql table in laravel 5.8

I am trying to compare two tables to check is user data already exits in another table. I show some posts they talk about whereRaw but details is not enough i was try to run query but i am not getting which i want.
UPDATED
I have 2 tables one is for challenges between two people
Challenge Table id,user_one, user_two, data_one, data_two,winner_id
And my second table is
vote Table id, user_id, battle_id, voted_user
I want to check is user already votes the table or not, If yes skip that challenge table and show remain table data to user.
$challenges = DB::table('challenges')->whereRaw([
['challenges.user_one', '!=', $uid],
['challenges.id', '!=', 'vote.id'],
])->orWhereRaw([
['challenges.user_one', '!=', $uid],
['challenges.id', '!=', 'vote.id'],
])->get();
You can achieve it by using crossJoin
$challenges = DB::table('challenges')->crossJoin('vote')
->where('challenges.user_one', '!=', $uid)
->where('challenges.id', '!=', 'vote.id')
->orWhere('challenges.user_one', '!=', $uid)
->orWhere('challenges.id', '!=', 'vote.id')->get();
Refer: https://laravel.com/docs/5.8/queries

Laravel query builder duplicates row

I'm printing some data in mi index.blade.php file but it returns duplicated values. This is my query:
$hist = DB::table('codigo_sisnova')
->join('llamada', 'codigo_sisnova.idPaciente', '=', 'llamada.id_paciente')
->join('medico', 'llamada.id_medico', '=', 'medico.id_medico')
->where('llamada.status_llamada', 'Finalizada')
->where(function($query){
$query->where('llamada.status_pago', '=', 'Sisnova')
->orWhere('llamada.status_pago', '=', 'RedireccionadaSisnova');
})
->distinct()
->get();
I already tried with unique() but it doesn't work too.
EDIT
The relations between tables are one to may from "codigo_sisnova" to "llamada", if I take out the join with the table "medico" the rows keep duplicating
Every row gets a duplicate
You will have multiple codigo_sisnova rows if you are joining tables that have multiple matches on that table. DISTINCT would not eliminate those since the joined data will make it non-distinct in those results. I would recommend trying to use groupBy() to eliminate the redundant rows.
You are missing the group_by statement. Assuming that you have an ID column in codigo_sisnova, besides adding it in a SELECT clause, you need to add it before your get() method :
$hist = DB::table('codigo_sisnova')
->select('codigo_sisnova.id')
->join('llamada', 'codigo_sisnova.idPaciente', '=', 'llamada.id_paciente')
->join('medico', 'llamada.id_medico', '=', 'medico.id_medico')
->where('llamada.status_llamada', 'Finalizada')
->where(function($query){
$query->where('llamada.status_pago', '=', 'Sisnova')
->orWhere('llamada.status_pago', '=', 'RedireccionadaSisnova');
})
->group_by('codigo_sisnova.id')
->get();

sort a Laravel collection on the behalf of four conditions

i have a comanies table like this in mysql.it has 3 columns. is_verified,is_platinum,platinum_start_date, end_date.
on the frontend, the data is showing in paginate format so that now I want to get the companies list sorted in only one variable.
just to show companies first verified then non verified then platinum etc
All Platinium and verified companies
All Platinium
All verified companies
All no verified and no platinum user.
I have tried by "orderBy","Sorting" etc but by using individual variable
$companies = DeveloperCompanies::where('is_approved', '=',
1)->orderBy('created_at', 'desc')->paginate(9);
here is a table format
use orderBy four times for each column
$companies = DeveloperCompanies::where('is_approved', '=', 1)
->orderBy('is_verified', 'desc')
->orderBy('is_platinium', 'desc')
->orderBy('platinium_start_date', 'desc')
->orderBy('platinium_end_date', 'desc')
->paginate(9);

Ordering a pagination by a relationship (Laravel 5.3)

In my app I have posts. I wish to show all the posts on the homepage, but order them higher if the post's user has uploaded an image. I can determine if the post's user has uploaded an image by checking if the user has a relationship with a row in the images table, like this:
$post->user->image
My code currently looks like this:
$posts = Post::where('subject_id', '=', $subject->id)
->approved()
->orderBy('created_at', 'desc')
->paginate(18);
Currently, I am simply ordering it by created at, but ideally all posts whose related user has an image will come first, then the rest. I've been looking for a way to do this efficiently and in a way that doesn't just work on the first page.
How should I go about this?
Try this:
$posts = Post::where('subject_id', '=', $subject->id)
->approved()
->select(['*', DB::raw('(SELECT count(images.id) FROM images INNER JOIN users ON users.image_id = images.id WHERE posts.user_id = users.id) as count_images'])
->orderBy('count_images', 'desc')
->orderBy('created_at', 'desc')
->paginate(18);
Try this:
$posts = Post::where('subject_id', '=', $subject->id)
->approved()
->with('user')
->join('users', 'users.id', '=', 'post.user_id')
->select(['post.*', 'users.avatar'])
->orderBy('image', 'desc')
->orderBy('created_at', 'desc') // ->latest() can be used for readability
->paginate(18);
This code eager loads users to reduce number of DB queries and doesn't use raw queries which should be avoided, because mistake can make your application vulnerable to SQL injection.
Explanation:
We eager load relationship using ->with('user') method, then join users table, select all fields from posts table and only image field from users table, then order results by image and paginate results.
Result should look like this:
App\Post:
id
title
...
image(from users table)
App\User (eager loaded relationship)
id
name
email
...
image
created_at

Laravel get my friend list query

I am using laravel to create a friend list. Now I want to get all of my friends with the name and avatar and stuff from the USER table.
My schema:
`cid`, `uid_by`, `uid_to`, `status`
uid_by (the inviting user)
uid_to (to be friend user)
status (accepted or ignored)
I only have 1 row per friendship. meaning its a 2 way friendship.
I tried to make a query:
$contacts = DB::table('contacts')
->leftJoin('users', 'contacts.uid_by', '=', 'users.uid')
->where('uid_by', $me)
->orWhere('uid_to', $me)
->where('contacts.status', 'accepted')
->get();
But its giving me some wrong information. Like instead of giving me the friend user info. its giving me my own info. All I want is to get the friends info.
Should I redesign the db? or am I missing something?
update
technically the thing is if uid_by is my user_id then check the uid_to else if the uid_by is not my user_id then check uid_by then use those to get the data from the users table uid column.
So my uid could be on uid_by and my friend on uid_to or vice versa.
But I can't get the query right.
See Advanced wheres:
$contacts = DB::table('contacts')
->join('users', 'contacts.uid_by', '=', 'users.uid')
->where('users.uid','<>',$me)
->where('contacts.status', 'accepted')
->where(function($query)
{
$query-->where('uid_by', $me)
->orWhere('uid_to', $me)
})
->get();

Categories