0

ive been trying to retrive some messages sent by ser from a chat table. and i came up with a query which is working quite well in phpmyadmin.

but when i use the same query in laravel it isn't working.

SELECT user2,message,created_at FROM ( SELECT user2,message,created_at FROM chats WHERE user1 ='Tiffany' UNION SELECT user1,message,created_at FROM chats WHERE user2 ='Tiffany' in ( SELECT DISTINCT user2 FROM chats WHERE user1 ='Tiffany' UNION SELECT distinct user1 FROM chats WHERE user2 ='Tiffany' ) )tb2 WHERE user2 !='Tiffany' GROUP BY user2

ive tried almost everything but could find a solution. please give me a small explanation why the above query isn't working. the fields of my chat table are

id,user1(varchar),user2(varchar),message,created_at(timestamp),updated_at(timestamp)

the whole code ive used

    if ($pmessage = \DB::select('select user2,message,created_at
  from (select user2,message,created_at from chats where user1 ='Tiffany'
  union select user1,message,created_at from chats where user2 ='Tiffany'
  in ( select distinct user2 from chats where user1 ='Tiffany'
  union select distinct user1 from chats where user2 ='Tiffany' ))
  where user2 != 'Tiffany'
  group by user2')) {
      return response()->json(['pmessage' => $pmessage, 'status' => 200], 200);
  } else {
      return response()->json(['status' => 505], 505);
  }
2
  • Are you using DB:: or Eloquent? Post your code please. Commented Feb 27, 2016 at 18:16
  • @AlexeyMezenin im using DB:: check the edited version Commented Feb 27, 2016 at 18:25

2 Answers 2

4

I think you need to use DB::raw(). Something like:

$pmessage = \DB::select(\DB::raw("SELECT ..."))

Also, you need to mix your quotes. So use double-quotes around your query, so that you can use single-quotes within your query. So you should be using something like this:

$pmessage = \DB::select( \DB::raw("Select user2,message,created_at
from (select user2,message,created_at from chats where user1 ='Tiffany'
union select user1,message,created_at from chats where user2 ='Tiffany'
in ( select distinct user2 from chats where user1 ='Tiffany'
union select distinct user1 from chats where user2 ='Tiffany' ))
where user2 != 'Tiffany'
group by user2')"));
Sign up to request clarification or add additional context in comments.

3 Comments

i did that but its still not working ive used postman to check it and its giving an error
Can you edit your question with your current code? Maybe include the error you are getting from postman?
ive its already edited and the error im getting on postman is Status 500 Internal Server Error 500 Internal Server Error
1

i root folder you have file .env after open with notepad you shold have command like this

**

APP_ENV=local
APP_DEBUG=true
APP_KEY=VTRcD5ZoIabuY9QR5Ih1QeCTTgCHQZrR

DB_HOST=localhost
DB_DATABASE=angulara
DB_USERNAME=root
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

also you must corect config DB in config/database.php

1 Comment

there's nothing wrong with the configurations ive just typed a simple select query and it works. so im pretty sure that configurations are done properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.