Skip to content

Commit

Permalink
change Conversations and messages model
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid committed Oct 6, 2015
1 parent 1f3ec86 commit 8b3aae9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/Model/Conversations.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ class Conversations extends Model {
protected $table="conversations";
public $timestamps=true;

public function messages()
{
return $this->hasMany('App\Messages', 'conversation_id');
}


public function getConversationList($user){
$conversations=DB::select(
DB::raw("SELECT user.id as userid, user.first_name, user.last_name, user.photo, conv.id as conv_id, msg.subject
FROM rent_rentastico_users user, rent_conversations conv, rent_messages msg
DB::raw("SELECT user.id as userid, user.name, conv.id as conv_id, msg.message
FROM ".DB::getTablePrefix().config('talk.user_table')." user, ".DB::getTablePrefix()."conversations conv, ".DB::getTablePrefix()."messages msg
WHERE conv.id = msg.conversation_id
AND (
conv.user_one ={$user}
OR conv.user_two ={$user}
) and (msg.created_at)
in (
SELECT max(msg.created_at) as created_at
FROM rent_conversations conv, rent_messages msg
FROM ".DB::getTablePrefix()."conversations conv, ".DB::getTablePrefix()."messages msg
WHERE CASE
WHEN conv.user_one ={$user}
THEN conv.user_two = user.id
Expand All @@ -41,10 +47,12 @@ public function getConversationList($user){
return $conversations;
}

public function getUserAuthConversation($conversationId){
public function getUserAuthConversation($conversationId, $user=null){
$user=!is_null($user)?$user:Auth::user()->id;

$check=$this->where('id',$conversationId)
->where(function($query){
$query->where('user_one', Auth::user()->id)->orWhere('user_two', Auth::user()->id);
->where(function($query) use ($user){
$query->where('user_one', $user)->orWhere('user_two', $user);
})->get()->count();

if($check<1) {
Expand Down
20 changes: 13 additions & 7 deletions src/Model/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Database\Eloquent\Model;
use DB;
use Auth;

class Messages extends Model {

Expand All @@ -11,14 +12,19 @@ class Messages extends Model {
public $timestamps=true;



public function readMessageFromSender($conversationId){
public function conversation()
{
return $this->belongsTo('App\Conversations');
}

public function getConversations($conversationId)
{
$readMessage=DB::select(DB::raw(
"SELECT U.photo, U.first_name, U.last_name, M.id, U.id as user_id, M.body, M.subject, M.created_at
FROM rent_rentastico_users U, rent_messages M
WHERE M.user_id = U.id
AND M.conversation_id = {$conversationId}
order by M.created_at asc"
"SELECT U.name, M.id, U.id as user_id, M.message, M.created_at
FROM ".DB::getTablePrefix()."users U, ".DB::getTablePrefix()."messages M
WHERE M.user_id = U.id
AND M.conversation_id = {$conversationId}
order by M.created_at asc"
));
return $readMessage;
}
Expand Down

0 comments on commit 8b3aae9

Please sign in to comment.