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();
Related
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
I have a to and from columns which are integers from the users in my messages table, as I was building a chat like application on this part.
So I want to fetch all the messages of the currently logged on user. so this is the query
$all_messages = Message::where('from',$logged_user)->orWhere('to',$logged_user)->with('owner')->orderBy('created_at','desc')->get();
note that $logged_user variable is an integer / the authenticated users id
so from that,
but also I want to get also the messages between the logged_user and also the respondent_id or the id of the respondent or the user that I am trying to connect also.
So for example the currently logged on user_id is 2 and the respondent id is 3 I want the messages that have a from of 2 or 3 and also the respondent_id of 2 and 3 something like that
If I've understood the question right you're looking for the messages belonging to the conversation between two users, so what you're looking for should be following:
$loggedInUserId = 2;
$contactUserId = 3;
$messages = Message::query()
->where(function ($query) use ($loggedInUserId, $contactUserId) {
$query->where('sender_id', $loggedInUserId)
->where('recipient_id', $contactUserId);
})
->orWhere(function ($query) use ($loggedInUserId, $contactUserId) {
$query->where('recipient_id', $loggedInUserId)
->where('sender_id', $contactUserId);
})
->with('owner')
->orderBy('created_at', 'desc')
->get();
Also, if you're heavily relying on having to track "conversations" between two or more users you might think to aggregate them as threads.
I have a query that will get the data from a joined tables. I successfully fetched the data from the 2 tables but for a long time, I did not notice that only one primary id of a certain table has been returned. I made adjustments but still never figure it out. What would I do? Please help. Thanks a lot guys. Here is my code.
$purchase = Purchase::where('purchases.purchase_order_id', $id)
->join('products', 'purchases.product_id', '=', 'products.id')
->select('purchases.*', 'products.*')
->get();
It only returns the primary id of a product, primary id of the table purchases is not included. What is the problem of the query above?
You can use select as:
->select('purchases.*', 'purchases.id as purchase_id', 'products.*', 'products.id as product_id')
This query returns the IDs of Survey Table as surveys_id as well as IDs of industries table along with all other data:
surveydata = Survey::select(
'surveys.*',
'surveys.id as surveys_id',
'industries.*',
'industries.id as industries_id'
)->where('surveys.active_status', '=','1')
-> join (
'industries',
'industries.id',
'=',
'surveys.survey_industry_id'
)->orderBy('surveys.id','desc')
->paginate(12);
I have three tables .
users
id name
roles
id name
role_user
id user_id role_id
I want to join them like
id name name(role) user_id role_id
My vision is to show the role name of the user.
I have tried this :
DB::table('users')
->join('role_user', 'users.id', '=', 'role_user.user_id')
->join('roles', 'role_user.role_id', '=', 'roles.id')
->select('users.id', 'role_user.role_id', 'roles.name')
->get();
Can you please tell me what I am doing wrong or if I am going thru a bad way.
Thanks in Advance
The name field is "double". This is not a problem for your database but it will not display well. Try the following code:
$result = DB::table('..etc.')->get(['users.*','role_user.role_id','roles.name as role_name']);
foreach($result as $row) {
print_r($row->toArray());
}
As you can see I've changed the get() call to make sure that the user's "name" field does not clash with the role's "name" field.
I inherited this Kohana project and have little experience with it and ORM.
Table structure is like this:
ROLES TABLE
id
name
ROLES_USERS TABLE
role_id
user_id
USERS TABLE
id
email
password
last_login
The thing is, I need to get users sorted by whether they have a certain role (login in this case) but have no idea how to do that with ORM.
Current query is:
$users = ORM::factory('user')
->limit($pagination->items_per_page)
->offset($pagination->offset)
->order_by('last_login', 'DESC')
->find_all();
and then when outputting it's printed like this:
$row['status'][] = ($user->has('roles', ORM::factory('role', array('name' => 'login')))
? '<span class="green">Active</span>'
: '<span class="red">Blocked</span>');
So the question would be how to alter the query to be able to sort by whether users are allowed to login or not.
$users = ORM::factory('user')
->join('roles_users', 'LEFT')
->on('roles_users.user_id', '=', 'user.id')
->join('roles', 'LEFT')
->on('roles_users.role_id', '=', DB::expr("roles.id AND roles.name = 'login'"))
->group_by('user.id')
->order_by('IFNULL("roles.id", \'2000\')', 'ASC')
->find_all()
I hope you don't have 2000 roles, but active users should come first using this query
Maybe you could just get the users for the login role?
$login_users = ORM::factory('role', array('name'=>'login'))->users->find_all();