Skip to content

Commit 2bb6f41

Browse files
committed
WIP - Initial conversation modal
1 parent 870535c commit 2bb6f41

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

app/Conversation.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Conversation extends Model
8+
{
9+
protected $fillable = [
10+
'created_by'
11+
];
12+
13+
/**
14+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
15+
*/
16+
public function users()
17+
{
18+
return $this->belongsToMany(User::class, 'conversation_user')
19+
->withTimestamps();
20+
}
21+
22+
/**
23+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
24+
*/
25+
public function messages()
26+
{
27+
return $this->hasMany(Message::class, 'conversation_id')
28+
->with('sender');
29+
}
30+
31+
/**
32+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
33+
*/
34+
public function last_message()
35+
{
36+
return $this->hasOne(Message::class)
37+
->orderBy('messages.id', 'desc')
38+
->with('sender');
39+
}
40+
41+
/**
42+
* @param $participants
43+
*
44+
* @return mixed
45+
*/
46+
public static function start($participants)
47+
{
48+
$conversation = self::create([
49+
'created_by' => auth()->id()
50+
]);
51+
52+
if ($participants) {
53+
$conversation->addParticipants($participants);
54+
}
55+
56+
return $conversation;
57+
}
58+
59+
/**
60+
* @param $userIds
61+
*
62+
* @return $this
63+
*/
64+
public function addParticipants($userIds)
65+
{
66+
$this->users()->attach(is_array($userIds) ? $userIds : [$userIds]);
67+
68+
return $this;
69+
}
70+
}

app/ConversationUser.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class ConversationUser extends Model
8+
{
9+
protected $table = 'conversation_user';
10+
11+
/**
12+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
13+
*/
14+
public function conversation()
15+
{
16+
return $this->belongsTo(Conversation::class, 'conversation_id');
17+
}
18+
}

app/Message.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Message extends Model
8+
{
9+
protected $fillable = [
10+
'body', 'user_id', 'type', 'is_seen'
11+
];
12+
13+
/**
14+
* All of the relationships to be touched.
15+
*
16+
* @var array
17+
*/
18+
protected $touches = ['conversation'];
19+
20+
protected $casts = [
21+
'is_seen' => 'boolean'
22+
];
23+
24+
/**
25+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
26+
*/
27+
public function sender()
28+
{
29+
return $this->belongsTo(User::class);
30+
}
31+
32+
/**
33+
* @param $user
34+
*
35+
* @return mixed
36+
*/
37+
public function unreadCount($user)
38+
{
39+
return Message::where('user_id', $user->id)
40+
->where('is_seen', 0)
41+
->count();
42+
}
43+
44+
/**
45+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
46+
*/
47+
public function conversation()
48+
{
49+
return $this->belongsTo(Conversation::class);
50+
}
51+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateMessagesTables extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('conversations', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->unsignedInteger('created_by')->nullable();
19+
$table->timestamps();
20+
21+
$table->foreign('created_by')
22+
->references('id')
23+
->on('users')
24+
->onDelete('cascade');
25+
});
26+
27+
Schema::create('messages', function (Blueprint $table) {
28+
$table->increments('id');
29+
$table->text('body');
30+
$table->integer('conversation_id')->unsigned();
31+
$table->integer('user_id')->unsigned();
32+
$table->string('type')->default('text');
33+
$table->boolean('is_seen')->default(false);
34+
$table->timestamps();
35+
36+
$table->foreign('user_id')
37+
->references('id')
38+
->on('users')
39+
->onDelete('cascade');
40+
41+
$table->foreign('conversation_id')
42+
->references('id')
43+
->on('conversations')
44+
->onDelete('cascade');
45+
});
46+
47+
Schema::create('conversation_user', function (Blueprint $table) {
48+
$table->integer('user_id')->unsigned();
49+
$table->integer('conversation_id')->unsigned();
50+
$table->primary([
51+
'user_id', 'conversation_id'
52+
]);
53+
$table->timestamps();
54+
55+
$table->foreign('conversation_id')
56+
->references('id')->on('conversations')
57+
->onDelete('cascade');
58+
59+
$table->foreign('user_id')
60+
->references('id')->on('users')
61+
->onDelete('cascade');
62+
});
63+
}
64+
65+
/**
66+
* Reverse the migrations.
67+
*
68+
* @return void
69+
*/
70+
public function down()
71+
{
72+
Schema::dropIfExists([
73+
'conversations', 'messages', 'conversation_user'
74+
]);
75+
}
76+
}

0 commit comments

Comments
 (0)