我的系统中具有消息传递功能,似乎只有其他发送者才能发送和查看消息。
我已经尝试调试,但是指向的ID是正确的。
以下是屏幕截图:
这是我的客户看到邮件的方式
这就是我的治疗师可以看到的
这是我的模特:
这是User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* Class User
* @package App
*
* @property $user_type string
*
*/
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
'user_type',
'status'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
protected $appends = [
'fullName'
];
public function therapist()
{
return $this->hasOne('App\Therapist', 'user_id', 'id');
}
public function client()
{
return $this->hasOne('App\Client', 'user_id', 'id');
}
public function getFullNameAttribute()
{
return "{$this->fname} {$this->lname}";
}
public function isRole($role)
{
return $this->user_type === $role;
}
public function homepage()
{
switch ($this->user_type) {
case 'therapist':
return redirect(route('get.therapist-account'));
case 'client':
return redirect(route('get.client-find'));
case 'admin':
return redirect(route('get.view'));
}
}
public function conversationWith($partner, $lastMessageId = false)
{
Message::query()
->where([
['sent_to', '=', $this->id],
['sent_from', '=', $partner],
])
->whereNull('seen_at')
->update([
'seen_at' => date_create(null)->format('Y-m-d H:i:s')
]);
$data = Message::query()
->where(function ($query) USE ($partner) {
$query->where(function ($q) USE ($partner) {
$q->where('sent_from', $this->id)->where('sent_to', $partner);
})->orWhere(function ($q) USE ($partner) {
$q->where('sent_from', $partner)->where('sent_to', $this->id);
});
})
->orderBy('created_at');
if ($lastMessageId) {
$data->where('id', '>', $lastMessageId);
} else {
$data->limit(20);
}
return $data->get();
}
public function getConversationList()
{
return \DB::table('messages')
->select(
'messages.sent_from AS sender_id',
\DB::raw('SUM(CASE WHEN messages.seen_at IS NULL THEN 1 ELSE 0 END) AS unseen_count')
)
->where('messages.sent_to', $this->id)
->groupBy('messages.sent_from')
->get();
}
}
此user_id指向这些角色,因为它们具有不同的角色
Client.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
protected $fillable = [
'user_id',
'fname',
'lname',
'contact',
'gender',
'city',
'province',
'street',
'brgy',
'res_detail',
'building',
'landmark',
'address_remarks',
];
protected $appends = [
'fullName',
'address'
];
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function scopeOfUser($query, $userId)
{
return $query->where('user_id', $userId);
}
public function booking()
{
return $this->hasMany('App\BookingRequest', 'client_id', 'user_id');
}
public function report()
{
return $this->hasMany('App\Report', 'client_id', 'user_id');
}
public function progress()
{
return $this->hasMany('App\Progress', 'client_id', 'user_id');
}
public function appointment()
{
return $this->hasMany('App\Appointment', 'client_id', 'user_id');
}
public function getFullNameAttribute()
{
return "{$this->fname} {$this->lname}";
}
public function getAddressAttribute()
{
return "{$this->res_detail} {$this->building} {$this->street} {$this->brgy} {$this->city} {$this->province} {$this->landmark} {$this->address_remarks} ";
}
public static function ofTherapist(int $therapistId)
{
$bookingRequests = BookingRequest::query()
->select('client_id')
->where([
['therapist_id', '=', $therapistId],
['status', '=', 1]
])->pluck('client_id');
return $bookingRequests->count()
? parent::whereIn('user_id', $bookingRequests->all())->get()
: collect();
}
}
Therapist.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Therapist extends Model
{
protected $fillable = [
'user_id',
'image',
'fname',
'lname',
'contact',
'gender',
'streetaddress',
'city',
'province',
'barangay',
'postal_code',
'longitude',
'latitude',
'therapist',
'license_number',
'expiry_date',
'license_image',
'nbi_image',
'bc_image',
'user_bio',
'personal_rate',
];
protected $appends = [
'full_name'
];
/**
*
*/
public function specialty()
{
return $this->belongsToMany('App\Specialty', 't_specialties', 'therapist_id', 'spec_id');
}
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function associatedUser()
{
return $this->belongsTo(User::class, 'user_id');
}
public function scopeOfUser($query, $userId)
{
return $query->where('id', $userId);
}
public function bookingRequest()
{
return $this->hasMany('App\BookingRequest', 'therapist_id', 'user_id');
}
public function report()
{
return $this->hasMany('App\Report', 'therapist_id', 'user_id');
}
public function appointment()
{
return $this->hasMany('App\Appointment', 'therapist_id', 'user_id');
}
public function appointments()
{
return $this->hasMany('App\Appointment', 'therapist_id', 'user_id');
}
public function approvedAppointments()
{
return $this->appointments()->whereHas('booking', function ($q) {
return $q->where('status', 1);
});
}
public function getFullNameAttribute()
{
return "{$this->fname} {$this->lname}";
}
public function getAddressAttribute()
{
return "{$this->streetaddress}, {$this->town}, {$this->barangay}, {$this->city}, {$this->province}";
}
public function specialties()
{
return $this->belongsToMany(Specialty::class, 't_specialties', 'therapist_id', 'spec_id');
}
public function accept()
{
return $this->associatedUser()->update([
'status' => 0
]);
}
}
这是我的消息传递控制器:
<?php
namespace App\Http\Controllers;
use App\Message;
use App\Therapist;
use Illuminate\Http\Request;
use App\Client;
use Auth;
class MessagingController extends Controller
{
public function index($recipientId = null)
{
$threadList = Auth::user()->getConversationList()->keyBy('sender_id');
if (Auth::user()->isRole('therapist')) {
$contacts = Client::ofTherapist(data_get(Auth::user(), 'therapist.user_id'));
} elseif (Auth::user()->isRole('client')) {
$contacts = Therapist::whereIn('user_id',$threadList->pluck('sender_id'));
}
// dd($contacts->toArray());
$thread = [];
if ($recipientId) {
$thread = Auth::user()->conversationWith($recipientId);
}
// dd($thread->toArray());
return view('therapist.message', compact('threadList', 'thread', 'contacts'));
}
public function sendMessage($recipientId, Request $request)
{
$request->validate([
'message' => 'required'
]);
Message::create([
'sent_from' => Auth::id(),
'sent_to' => $recipientId,
'message' => $request->input('message')
]);
return redirect()->back();
}
}
这是我的观点。刀片
@extends('layouts.both')
@section('title', 'Messaging')
@section('page-section')
@php $recipient = request()->route('recipientId') @endphp
<link rel="stylesheet" type="text/css" href="{{ asset('css/chat.css') }}">
<!------ Include the above in your HEAD tag ---------->
<div class="messaging-below">
<h3 class="text-center">Messaging</h3>
<div class="messaging">
<div class="inbox_msg">
<div class="inbox_people">
<div class="headind_srch">
<div class="recent_heading">
Recent
</div>
<div class="srch_bar">
<div class="stylish-input-group">
<input type="text" class="search-bar" placeholder="Search">
<span class="input-group-addon">
<button type="button"> <i class="fa fa-search" aria-hidden="true"></i> </button>
</span></div>
</div>
</div>
<div class="inbox_chat">
@foreach($contacts AS $contact)
<div class="chat_list {{ $recipient == $contact->user_id ? 'active_chat' : '' }}"
style="cursor:pointer;"
onclick="javascript:window.location.href = '{{ route('messaging.index', $contact->user_id) }}'">
<div class="chat_people align-items-center d-flex">
<div class="chat_img text-info">
<i class="fa fa-user-circle fa-2x"></i>
</div>
<div class="chat_ib">
<h5 class="">{{ $contact->fullname }}
@if(optional($threadList->get($contact->user_id))->unseen_count)
<span class="badge badge-pill badge-secondary chat_date">{{ optional($threadList->get($contact->user_id))->unseen_count }}</span>
@endif
</h5>
</div>
</div>
</div>
@endforeach
</div>
</div>
<div class="mesgs bg-white h-100">
@if($recipient)
<div class="msg_history" id="message-history">
@foreach($thread as $message)
@if($message->sent_from == Auth::id())
<div class="outgoing_msg">
<div class="sent_msg">
<p>{{ $message->message }}</p>
<span class="time_date">{{ $message->created_at->diffForHumans(Carbon\Carbon::now()) }}</span>
</div>
</div>
@else
<div class="incoming_msg">
<div class="incoming_msg_img"> <i class="fa fa-user-circle fa-2x"></i></div>
<div class="received_msg">
<div class="received_withd_msg">
<p>{{ $message->message }}</p>
<span class="time_date">{{ $message->created_at->diffForHumans(Carbon\Carbon::now()) }}</span></div>
</div>
</div>
@endif
@endforeach
</div>
<div class="type_msg">
{!! Form::open(['url' => route('messaging.send', $recipient), 'method' => 'post']) !!}
<div class="input_msg_write">
{!! Form::inputGroup('text', null, 'message', null, ['placeholder' => 'Type a message...']) !!}
<button class="msg_send_btn" type="submit"><i class="fa fa-paper-plane"></i></button>
</div>
{!! Form::close() !!}
</div>
@else
<p class="text-info text-center">Choose a recipient from the left</p>
@endif
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
<script type="text/javascript">
$(document).ready(function(){
var objDiv = document.getElementById("message-history");
if(objDiv){
objDiv.scrollTop = objDiv.scrollHeight;
}
$('.submit').click(function (e){
e.preventDefault();
var $this = $(this)
$this.closest('form').attr('action', $(this).data('url'));
$this.closest('form').submit();
})
// $('.update-appointment').submit(function(e) {
// e.preventDefault();
// console.log(e);
// })
})
</script>
@endpush
这应该允许两个用户互相发送消息,但只有一个视图在起作用,这是治疗师方面的观点。
所有数据都可以插入数据库,但是在我的客户看来,她无法查看和发送消息。