i've implemented notifications on my app.And they were working fine until yesterday out of the blue the stopped working.now when i click on the route that enables the notification it just keeps loading this is the store method on my controller where i check if the email notifications are enabled and if they are the user should recieve an email everytime a project is published
public function store(Request $request){
$this->validate(request(), [
'title' => 'required|max:255|unique:projects',
'body' => 'required|max:1000',
// 'tutorial' => 'required|max:1000',
'avatar' => 'required|mimes:jpeg,bmp,png',
'zip_file' => 'required|mimes:zip,rar',
]);
$user = User::all();
$profiles_storage = storage_path('app/avatars/');
$project = new Project;
$project -> user_id = auth()->id();
$project -> title = request('title');
$project -> body = request('body');
$project -> tutorial = request('tutorial');
$project -> views = '0';
$project -> downloads = '0';
$project -> alternative_text = config('app.name').' '.request('title').' '.'project';
$profile = request()->file('avatar');
// $profile->store('profiles');
$project->image = $profile->hashName();
$image = Image::make($profile->getRealPath());
$image->fit(640, 360, function ($constraint)
{ $constraint->upsize();})->save($profiles_storage.$profile->hashName());
request()-> file('zip_file')->store('zip_files');
$project -> zip_file = request()->file('zip_file')->hashName();
$project -> save();
$category = $request->input('categories');
$project->categories()->sync($category);
foreach ($user as $u) {
if ($u->email_notifications != 0) {
$u->notify(new ProjectPublished($project));
}
}
session()->flash('message',"{$project->title}".' created');
return redirect('/admin/projects');
}
and this is the ProjectPublished.php
<?php
namespace App\Notifications;
use App\User; use App\Project; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage;
class ProjectPublished extends Notification { use Queueable; protected $project;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(Project $project)
{
$this->project = $project;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello there')
->subject('New Project:'.$this->project->title)
->line('As requested we are letting you know that a new project was published at Rek Studio')
->action('Check it out', url('projects/'.$this->project->title));
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
can anyone help me out?
Related
I created a localized copy of ResetPassword.php, redefined the sendPasswordResetNotification () method in app\user. In general, I did everything according to the rule (I think so))). All the rules, sends a letter to the mail but there is only one problem. This link does not expire. by default there is 60 minutes, after which the link should be destroyed. But this does not happen. Below my code will be grateful for any help.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class mytelresetpassword extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($token)
{
//
$this->token = $token;
}
/**
* The callback that should be used to build the mail message.
*
* #var \Closure|null
*/
public static $toMailCallback;
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
if (static::$toMailCallback) {
return call_user_func(static::$toMailCallback, $notifiable, $this->token);
}
$time = ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')];
$time = $time['count'];
return (new MailMessage)
->subject('Şifrənin Dəyişdirilməsi')
->line('Hesabınızdan şifrə yeniləmə tələbi aldığımız üçün bu mail sizə göndərilib.')
->action('Şifrəni Yenilə', url(config('app.url').route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
->line('Bu link '.$time.' dəqiqə müddətində keçərlidir, bu müddət bitdikdən sonra deaktiv olunacaq!')
->line('Şifrə dəyişməyi tələb etməmişsinizsə, "Şifrəni Yenilə" düyməsini klikləməyə ehtiyac yoxdur.');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
/**
* Set a callback that should be used when building the notification mail message.
*
* #param \Closure $callback
* #return void
*/
public static function toMailUsing($callback)
{
static::$toMailCallback = $callback;
}
}
in app\user:
public function sendPasswordResetNotification($token)
{
$this->notify(new mytelresetpassword($token));
}
I propose adding a few lines in your ResetPasswordController.php:
public function showResetForm(Request $request, $token = null)
{
$email = $request->email;
if (!$token)
return redirect()->route('login');
if (!$email)
return redirect()->route('login');
$reset = PasswordReset::where('email', $request->input('email'))->first();
if (!$reset)
return redirect()->route('login');
$expiry = Carbon::now()->subMinutes( 60 );
if ($reset->created_at <= $expiry) {
return redirect()->route('login');
}
return view('auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
error
enter image description here
I am trying to send notifications of the event when some likes and comment on his post, notifications for comments and likes working
here is my notification class.
i have error in my CommentController if ($event->user_id != $comment->user_id)
class NewCommentEvent extends Notification
{
use Queueable;
protected $comment;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($comment)
{
$this->comment = $comment;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toDatabase($notifiable)
{
return [
'comment' => $this->comment,
'event' => Event::find($this->comment->event_id),
'user' => User::find($this->comment->user_id)
];
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
My controller function code for notifications on comments
public function store(CommentRequest $request)
{
$event = Event::findOrFail($request->event_id);
Comment::create([
'comment' => $request->comment,
'user_id' => Auth::id(),
'event_id' => $event->id
]);
if ($event->user_id != $comment->user_id) {
$user = User::find($event->user_id);
$user->notify(new NewCommentEvent($comment));
}
Toastr::success('Comment post with success','', ["positionClass" => "toast-top-center"]);
return redirect()->back();
}
my CommenRequest
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class CommentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'comment' => 'required|max:2000',
];
}
}
In your controller: the variable $comment is not defined.
From Laravel docs:
The create method returns the saved model instance.
so the solution is:
$comment = Comment::create([
'comment' => $request->comment,
'user_id' => Auth::id(),
'event_id' => $event->id
]);
you have not defined your $comment, you just created a comment. This is throwing the error
$comment = Comment::create([
.
.
]);
This will fix your issue
The error message was clear. $comment is undefined. Replace your controller code with the follow:
public function store(CommentRequest $request)
{
$event = Event::findOrFail($request->event_id);
// defined comment here
$comment = Comment::create([
'comment' => $request->comment,
'user_id' => Auth::id(),
'event_id' => $event->id
]);
if ($event->user_id != $comment->user_id) {
$user = User::find($event->user_id);
$user->notify(new NewCommentEvent($comment));
}
Toastr::success('Comment post with success','', ["positionClass" => "toast-top-center"]);
return redirect()->back();
}
I'm making a function that each time I add a comment to a Support ticket it sends an email to the user but I'm getting getting Trying to get property of non-object when I submit the comment this only happens if I'm logged out of the users account and only logged into the Admin user
AdminComment Controller Code
<?php
namespace App\Http\Controllers;
use App\Comment;
use App\Mailers\AppMailer;
use App\Ticket;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminCommentController extends Controller
{
public function postComment(Request $request, AppMailer $mailer)
{
$this->validate($request, [
'comment' => 'required',
]);
$comment = Comment::create([
'ticket_id' => $request->input('ticket_id'),
'user_id' => Auth::user()->id,
'comment' => $request->input('comment'),
]);
$mailer->sendTicketComments($comment->ticket->user, Auth::user(), $comment->ticket, $comment);
return redirect()->back()->with('warning', 'There was a problem sending your comment to the customer via email');
}
}
here is the Mailer Controller
<?php
namespace App\Mailers;
use App\Status;
use App\Ticket;
use Illuminate\Contracts\Mail\Mailer;
class AppMailer
{
protected $mailer;
/**
* email to send to.
*
* #var [type]
*/
protected $to;
/**
* Subject of the email.
*
* #var [type]
*/
protected $subject;
/**
* view template for email.
*
* #var [type]
*/
protected $view;
/**
* data to be sent along with the email.
*
* #var array
*/
protected $data = [];
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
/**
* Send Ticket information to the user.
*
* #param User $user
* #param Ticket $ticket
*
* #return method deliver()
*/
public function sendTicketInformation($user, Ticket $ticket)
{
$statuses = Status::all();
$this->to = $user->email;
$this->subject = "TechWiseDirect Support Ticket - [Reference #: $ticket->ticket_id]";
$this->view = 'users.emails.ticket_info';
$this->data = compact('user', 'ticket', 'statuses');
return $this->deliver();
}
/**
* Send Ticket Comments/Replies to Ticket Owner.
*
* #param User $ticketOwner
* #param User $user
* #param Ticket $ticket
* #param Comment $comment
*
* #return method deliver()
*/
public function sendTicketComments($ticketOwner, $user, Ticket $ticket, $comment)
{
$this->to = $ticketOwner->email;
$this->subject = "RE:[Ticket ID: $ticket->ticket_id]";
$this->view = 'users.emails.ticket_comments';
$this->data = compact('ticketOwner', 'user', 'ticket', 'comment');
return $this->deliver();
}
/**
* Do the actual sending of the mail.
*/
public function deliver()
{
$this->mailer->send($this->view, $this->data, function ($message) {
$message->from('test#test')
->to($this->to)->subject($this->subject);
});
}
}
the error happens for the Auth::user()->id I think. it's because you are logged out of your account. my guess is that you haven't used guard and only have one main user model defined for laravel.
I am trying to send notifications to the owner of the post when some like and comment on his post, notifications for comments are working but when I do the same work for likes it is not working.
here is my notification class
<?php
namespace App\Notifications;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class show_notification extends Notification
{
use Queueable;
protected $comment;
protected $likes;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($comment,$likes)
{
$this->comment = $comment;
$this->likes = $likes;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toDatabase($notifiable)
{
return [
'comment' => $this->comment,
'likes' => $this->likes,
'user' => auth()->user()
//'repliedTime' => Carbon::now()
];
}
public function toArray($notifiable)
{
return [
//
];
}
}
My controller function code for notifications on comments
$id = $comment->post_id;
$getuser = Post::where('id', $id)->first();
$userid = $getuser->user_id;
if ($userid != $user_id ){
$user = User::where('id', $userid)->first();
$user->notify(new show_notification($comment));
}
My controller function code for notifications on likes
$id = $likes->post_id;
$getuser = Post::where('id', $id)->first();
$userid = $getuser->user_id;
if ($userid != $user_id ){
$user = User::where('id', $userid)->first();
$user->notify(new show_notification($likes));
}
just now I get this issues that is bothering me.
the error in code after validator::make in update function.
BadMethodCallException in Controller.php line 107: Method [all] does
not exist.
This is the full code from BooksController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\book;
class BooksController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$book = BooksController::all();
return view('book.index')->with('book', $book);
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return view('book.create');
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
$rules = array(
'judul' => 'required',
'author' => 'required',
'penerbit' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
// process the login
if ($validator->fails()) {
return Redirect::to('book/create')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
// store
$book = new book;
$book ->judul = Input::get('judul');
$book ->author = Input::get('author');
$book ->penerbit = Input::get('penerbit');
$book ->save();
// redirect
Session:flash('message', 'Berhasil membuat buku!');
return Redirect::to('book');
}
}
/**
* Display the specified resource.
*
* #param int $idate
* #return Response
*/
public function show($id)
{
$book = books::find($id);
return view('book.show')
->with('book', $book);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
$book = books::find($id);
return view('book.edit')
->with('book', $book);
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
$rules = array(
'judul' => 'required',
'author' => 'required',
'penerbit' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('book/' . $id . '/edit')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
// simpan
$book = books::find($id);
$book->judul = Input::get('judul');
$book->author = Input::get('author');
$book->penerbit = Input::get('penerbit');
$book->save();
// redirect
Session::flash('message', 'Berhasil mengganti info buku!');
return Redirect::to('book');
}
}
/**
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
$book = books::find($id);
$book ->delete();
//redirect
Session::flash('message', 'Berhasil menghapus buku!');
return Redirect::to('book');
}
}
try this use Validator; instead of
use Illuminate\Support\Facades\Validator;
convert user Input::all() to input()->all() or request()->all()