messages controller or received page does not work as the open button does not open the conversation that is in the received page.
I'm trying to make it work but I do not know why it does not work. I really do not have an explanation for why it does not work. what should I fix and where?
btw. there is probably an error(i think it is there as the open button does not work for some reason) in the received.blade.php file
this is the files of the messages:
app/Message.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Message extends Model
{
public function To(){
return $this->belongsTo('App\User','to');
}
public function From(){
return $this->belongsTo('App\User','from');
}
}
app/Http/Controllers/MessageController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\User;
use App\Message;
class MessageController extends Controller
{
public function SendView($username = null){
$user = Auth::user();
$recipient = User::where('username',$username)->first();
return view('message.send')->with([
'recipient' => $recipient
]);
}
public function SendPost(Request $request){
if ($request->recipient == null) {
session()->flash('errormessage','You must enter recipient\'s username');
return redirect()->back()->withInput();
}
if ($request->text == null) {
session()->flash('errormessage','You must send something');
return redirect()->back()->withInput();
}
if ($request->title == null) {
session()->flash('errormessage','You must enter title');
return redirect()->back()->withInput();
}
$recipient = User::where('username',$request->recipient)->first();
if ($recipient == null) {
session()->flash('errormessage','User with that username does not exist');
return redirect()->back()->withInput();
}
if ($recipient->id == Auth::user()->id) {
session()->flash('errormessage','You cannot send message to yourself');
return redirect()->back()->withInput();
}
$last_msg = Message::where('from',Auth::user()->id)->orderBy('created_at','desc')->first();
if ($last_msg !== null) {
$newTime = date("Y-m-d H:i:s",time());
if (Auth::user()->vendor == false) {
$last_time = date('Y-m-d H:i:s',strtotime('+1 minute',strtotime($last_msg->created_at)));
if ($newTime < $last_time) {
session()->flash('errormessage','You can send message every minute');
return redirect()->back()->withInput();
}
} else {
$last_time = date('Y-m-d H:i:s',strtotime('+10 seconds',strtotime($last_msg->created_at)));
if ($newTime < $last_time) {
session()->flash('errormessage','You can send message every ten seconds');
return redirect()->back()->withInput();
}
}
}
$m = new Message;
$m->uniqueid = 'ME'.str_random(28);
$m->to = $recipient->id;
$m->from = Auth::user()->id;
$m->title = $request->title;
$m->text = $request->text;
$m->save();
return redirect()->route('messages');
}
public function ViewReceived(){
$received = Message::where('to',Auth::user()->id)->orderBy('created_at','desc')->paginate(25);
return view('message.received')->with([
'received'=>$received
]);
}
public function ViewSent(){
$sent = Message::where('from',Auth::user()->id)->orderBy('created_at','desc')->paginate(25);
return view('message.sent')->with([
'sent'=>$sent
]);
}
public function ViewMessage($uniqueid){
$message = Message::where('uniqueid',$uniqueid)->first();
if ($message == null) {
return redirect()->route('messages');
}
if (Auth::user()->id !== $message->to && Auth::user()->id !== $message->from) {
return redirect()->route('messages');
}
if ($message->viewed == false && Auth::user()->id == $message->to) {
$message->viewed = true;
$message->save();
}
return view('message.view')->with([
'message'=>$message
]);
}
}
routes/web.php:
Route::get('message/send/{username?}','MessageController@SendView')->name('sendmessage');
Route::post('message/send','MessageController@SendPost')->name('sendmessagepost');
Route::get('messages/received','MessageController@ViewReceived')->name('messages');
Route::get('messages/sent','MessageController@ViewSent')->name('messagessent');
Route::get('message/view/{uniqueid}','MessageController@ViewMessage')->name('viewmessage');
resources/views/message/received.blade.php:
@extends('master.main')
@section('main-content')
@component('master.notification')
@slot('size')
col-md-8 col-md-offset-2
@endslot
@slot('title')
My messages
@endslot
<div class="row">
<center>
<a href="" class="btn btn-primary">Send new message</a>
</center>
</div>
<div class="row">
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="">Received</a></li>
<li role="presentation"><a href="">Sent</a></li>
</ul>
</div>
<table class="table table-hover">
<thead>
<th>From</th>
<th>Title</th>
<th>Time</th>
<th>Action</th>
</thead>
<tbody>
@foreach($received as $r)
<tr>
<td> @if($r->viewed == false)<span class="label label-default">didnt read it yet</span> @endif</td>
<td></td>
<td></td>
<td><a href="" class="btn btn-default">Open</a></td>
</tr>
@endforeach
</tbody>
</table>
<div class="form-group">
<center>
</center>
</div>
@endcomponent
@stop
resources/views/message/view.blade.php:
@extends('master.main')
@section('main-content')
@component('master.notification')
@slot('size')
col-md-8 col-md-offset-2
@endslot
@slot('title')
Message from <span class="font-weight-600"></span> sent to <span class="font-weight-600"></span>
@endslot
<div class="from-group">
</div>
<div class="form-group">
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="" class="form-control" readonly="">
</div>
<div class="form-group">
<label for="text">Text:</label>
<textarea name="text" class="form-control" style="resize:none" rows="8" cols="80" readonly=""></textarea>
</div>
<div class="form-group">
<center>
@if(Auth::user()->id == $message->To->id)
<a href="" class="btn btn-primary">Reply</a>
@elseif(Auth::user()->id == $message->From->id)
<a href="" class="btn btn-primary">Send another message to </a>
@endif
</center>
</div>
@endcomponent
@stop
I'm using laravel 5.4 and everything else in project is working perfectly fine. only this open button in the received page does not work.
via
Chebli Mohamed