一、dialog_id
message 表添加 dialog_id
php artisan make:migration add_dialog_id_to_messages --table=messages
修改database/migrations/add_dialog_id_to_messages.php
public function up()
{
Schema::table('messages', function (Blueprint $table) {
$table->bigInteger('dialog_id')->default(0);
});
}
public function down()
{
Schema::table('messages', function (Blueprint $table) {
$table->dropColumn(['dialog_id']);
});
}
执行数据迁移
php artisan migrate
修改 app/Message.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Message extends Model
{
protected $table = 'messages';
protected $fillable = ['from_user_id','to_user_id','content','dialog_id'];
public function fromUser()
{
return $this->belongsTo(User::class,'from_user_id');
}
public function toUser()
{
return $this->belongsTo(User::class,'to_user_id');
}
}
修改 app/Http/Controllers/MessagesController.php
public function store()
{
$message = $this->messageRepository->create([
'to_user_id' => request('user'),
'from_user_id' => user('api')->id,
'content' => request('content'),
'dialog_id' => time().Auth::id(),
]);
if($message){
$status = true;
}else{
$status = false;
}
return response()->json(['status'=>$status]);
}
二、创建控制器
创建收件箱控制器
php artisan make:controller InboxController
修改 routes/web.php
Route::get('inbox', 'InboxController@index');
Route::get('inbox/{dialogId}', 'InboxController@show');
Route::post('inbox/{dialogId}/store', 'InboxController@store');
修改 app/Repositories/MessageRepository.php
namespace App\Repositories;
use App\Message;
class MessageRepository
{
public function create(array $attributes)
{
return Message::create($attributes);
}
public function getMessageList()
{
return Message::where('to_user_id',user()->id)
->orWhere('from_user_id',user()->id)
->with(['fromUser','toUser'])
->get()
->unique('dialog_id')
->groupBy('to_user_id');
}
public function getMessageListByDialogId($dialogId)
{
return Message::where('dialog_id', $dialogId)->latest()->get();
}
public function store($dialogId)
{
$message = Message::where('dialog_id', $dialogId)->first();
$toUserId = $message->from_user_id === user()->id ? $message->to_user_id : $message->from_user_id;
return Message::create([
'from_user_id' => user()->id,
'to_user_id' => $toUserId,
'content' => request('content'),
'dialog_id' => $dialogId
]);
}
}
修改 app/Http/Controllers/InboxController.php
namespace App\Http\Controllers;
use App\Repositories\MessageRepository;
class InboxController extends Controller
{
protected $messageRepository;
public function __construct(MessageRepository $messageRepository)
{
$this->messageRepository = $messageRepository;
$this->middleware('auth');
}
public function index()
{
$messages = $this->messageRepository->getMessageList();
return view('inbox.index', compact('messages'));
}
public function show($dialogId)
{
$messages = $this->messageRepository->getMessageListByDialogId($dialogId);
return view('inbox.show', compact('messages','dialogId'));
}
public function store($dialogId)
{
$this->messageRepository->store($dialogId);
return back();
}
}
三、创建视图
修改 resources/assets/css/style.css
.message-list {
margin-top: 80px;
}
.a-avatar {
width: 75px;
display: block
}
.avatar {
border-radius: 50%;
}
执行 gulp
gulp
新建 resources/views/inbox/index.blade.php
新建 resources/views/inbox/show.blade.php
访问:http://127.0.0.1:8000/inbox 进行测试