Laravel 5.7 sending notifications using Expo - laravel

I'm using Laravel 5.7 for my backend (I'm new to Laravel) and I'm trying to use Expo push notification extension for Laravel to send notifications to my users.
I followed the steps explained, but I get lost to where I'm suppose to place the class ExpoNotification extends Notification and how to call it.
What I expect to happen is that every time an orders status get changed a notification is send to the user.
What is happening is that I get an error saying the class can't be found.
OrderController
public function update_order(Request $request, $id)
{
//Get the Order and update the status
Order::where('id', '=', $id )->update($request->only(['status']));
//Get the order with ::find so I can use $order->
$order = Order::find($id);
//Get user belonging to this order
$user= User::where('id', '=', $order->user_id);
//Get response with orders only posted the same day and are payed
$orders = Order::where('store_id', '=', $order->store_id)
->where('day', '=', $order->day )
->where('week', '=', $order->week )
->where('year', '=', $order->year )
->where('payment_status', '=', $order->payment_status)->get();
//send expo notification so the user gets his update
new ExpoNotification($order);
//return only relevant orders to the store
return OrderResource::collection($orders);
}
ExpoNotification
<?
namespace App\Notifications\ExpoNotification;
use App\Order;
use App\User;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
use Illuminate\Notifications\Notification;
class ExpoNotification extends Notification
{
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->enableSound()
->body("Your {$notifiable->service} account was approved!");
}
}
Error from postman
<!DOCTYPE html><!--
Symfony\Component\Debug\Exception\FatalThrowableError: Class 'App\Notifications\ExpoNotification' not found in file /Users/salmanmohamed/Documents/apps/rapiobackend/app/Http/Controllers/OrderController.php on line 182
Stack trace:
1. Symfony\Component\Debug\Exception\FatalThrowableError->() /Users/salmanmohamed/Documents/apps/rapiobackend/app/Http/Controllers/OrderController.php:182
Answer
Provided by Mohammed
<?php
namespace App\Notifications;
use App\Order;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
class ExNotification extends Notification
{
use Queueable;
protected $order;
public function __construct($order){
$this->order=$order;
}
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->enableSound()
->body("Your {$notifiable->service} account was approved!");
}
public function toArray($notifiable)
{
return [
//
];
}
}

Your wrong is your implementation of your ExpoNotification class it's namespace is App\Expo and you are using App\Notifications\ExpoNotification

Related

How to pass variable from Controller to Nova Recource?

I want to pass $defaultFrom from NewsletterController.php:
<?php
namespace App\Http\Controllers;
use App\Mail\NewsletterMail;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class NewsletterController extends Controller
{
public function send()
{
$defaultFrom = 'newsletter#stuttard.de';
DB::table('newsletter_mails')->insert(['from' => $defaultFrom]);
$emails = DB::select('select * from newsletters order by id desc');
foreach ($emails as $email) {
Mail::to($email)->send(new NewsletterMail());
}
}
}
to NewsletterMail.php:
<?php
namespace App\Nova;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
class NewsletterMail extends Resource
{
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'id')->sortable(),
Text::make('From', 'from')->default($defaultFrom)->placeholder($defaultFrom),
];
}
}
I've tried to put public $defaultFrom; above the fields() function or call new NewsletterMail($defaultFrom) but this seems to be wrong syntax. Sorry, I'm a bit new to Laravel.
I assume that you have Newsletter model. Move $defaultFrom to model as public const DEFAULT_FROM = 'newsletter#stuttard.de';. After doing this, you can call it's value in both places using Newsletter::DEFAULT_FROM.

Call to undefined method in seeder, Laravel

I try to create roles and permissions for users, and I get an error on seeder when I try to assign permissions to roles.
//from seeder
use App\Ability;
use App\Role;
use App\User;
public function run(){
$owner = Ability::where('name', '=', 'owner');
$administrator = Role::where('name', '=', 'administrator');
$administrator->allowTo($owner);
}
//from my Role Model
public function abilities()
{
return $this->belongsToMany(Ability::class)->withTimestamps();
}
public function allowTo($ability)
{
$this->abilities()->save($ability);
}
Call to undefined method
Illuminate\Database\Eloquent\Builder::allowTo()
You are not getting the data you are just checking the condition .
Try this and let me know if this helps you
//from seeder
use App\Ability;
use App\Role;
use App\User;
public function run(){
$owner = Ability::where('name', '=', 'owner')->first();
$administrator = Role::where('name', '=', 'administrator')->first();
$administrator->allowTo($owner);
}
//from my Role Model
public function abilities()
{
return $this->belongsToMany(Ability::class)->withTimestamps();
}
public function allowTo($ability)
{
$this->abilities()->save($ability);
}

Class App\Http\Controllers\Admin\Point\RedeemRequestsBoController does not exist error

Use Laravel 5.6 and I want to create page booking room in my website management system.
Here's my code:
Route
Route::namespace('Point')->group(function () {
Route::get('/Admin/Point/redeem-requestsbo', 'RedeemRequestBoController#index');
// Crm Booking Room
Route::delete('redeem-requestsbo/destroy', 'RedeemRequestsBoController#massDestroy')->name('redeem-requestsbo.massDestroy');
Route::resource('redeem-requestsbo', 'RedeemRequestsBoController');
Controller
namespace App\Http\Controllers\Admin\Point;
use App\CrmCustomer;
use App\Models\Point\RedeemBo;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use Symfony\Component\HttpFoundation\Response;
use App\Http\Requests\Point\StoreRedeemRequestBoRequest;
use App\Http\Requests\Point\UpdateRedeemRequestBoRequest;
use App\Models\Point\RoomType;
use App\Models\Point\SpecialRate;
use Carbon\Carbon;
class RedeemRequestBoController extends Controller
{
public function index()
{
abort_if(Gate::denies('view redeem request'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$roles = auth()->user()->roles->pluck('title')->toArray();
$admin = in_array('Admin', $roles);
if(!$admin) {
$rows = RedeemBo::whereHas('crmCustomer', function($query){
$query->where('email', auth()->user()->email);
})->get();
} else {
$rows = RedeemBo::all();
}
return view('admin.point.redeem-requestsbo.index', compact('rows'));
}
But I got error
Class App\Http\Controllers\Admin\Point\RedeemRequestsBoController does not exist error
booking room page
file folder structure
RedeemRequestsBoController#massDestroy does not exist because the Controller class name is RedeemRequestBoController. RedeemRequest with the (s)

Problems trying to send email with Laravel 5.5

I made an API with Laravel and it is registering the data correctly
Now I'm trying to get this to trigger registration confirmation emails, however these are not going
I'm using Mailtrap for the tests
In .env I put like this:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=291ac7fdcf52bb
MAIL_PASSWORD=610b2e5e9782d3
No Http/Mail/DataManifestMail.php tenho:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class DataManifestMail extends Mailable
{
use Queueable, SerializesModels;
protected $inputs;
public function __construct($inputs)
{
$this->inputs = $inputs;
}
public function build()
{
return $this->view('mails.data');
}
}
In views/mails/data.blade.php I only have one warning message:
<h1>Test e-mail</h1>
Then in my Http/Controller/ManifestationController.php I put it like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Manifestation;
use App\Mail\DataManifestationMail;
use Mail;
class ManifestationController extends Controller
{
private $manifestation;
public function __construct(Manifestation $manifestation)
{
$this->manifestation = $manifestation;
}
public function store(Request $request)
{
$nr = Manifestation::max('nrmanifestation');
$this->manifestation->nrmanifestation = $nr + 1;
$this->manifestation->dspass = $request->dspass;
$this->manifestation->eeemail = $request->eeemail;
$this->manifestation->address = $request->address;
$this->manifestation->name = $request->name;
$this->manifestation->latitude = $request->latitude;
$this->manifestation->longitude = $request->longitude;
$this->manifestation->save();
Mail::to('vazag#c1oramn.com')->send(new DataManifestationMail());
return response()->json([
'result' =>
$this->manifestation->nrmanifestation
]);
}
}
I have reread the documentation and my code several times and have not found any errors
What can it be?
There might be multiple reasons:
configuration is cached (use php artisan config:cache)
you used invalid mailtrap data (you should have log in laravel.log file)
your server has somehow blocked 2525 port
Also looking at your code it seems you miss passing $input parameter to constructor. You have:
public function __construct($inputs)
but you run:
->send(new DataManifestationMail());
without passing any value

Call to undefined method Illuminate\Notifications\Notification::send()

I am trying to make a notification system in my project.
These are the steps i have done:
1-php artisan notifications:table
2-php artisan migrate
3-php artisan make:notification AddPost
In my AddPost.php file i wrote this code:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class AddPost extends Notification
{
use Queueable;
protected $post;
public function __construct(Post $post)
{
$this->post=$post;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'data'=>'We have a new notification '.$this->post->title ."Added By" .auth()->user()->name
];
}
}
In my controller I am trying to save the data in a table and every thing was perfect.
This is my code in my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
//use App\Notifications\Compose;
use Illuminate\Notifications\Notification;
use DB;
use Route;
class PostNot extends Controller
{
public function index(){
$posts =DB::table('_notification')->get();
$users =DB::table('users')->get();
return view('pages.chat',compact('posts','users'));
}
public function create(){
return view('pages.chat');
}
public function store(Request $request){
$post=new Post();
//dd($request->all());
$post->title=$request->title;
$post->description=$request->description;
$post->view=0;
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
return redirect()->route('chat');
}
}
Everything was good until I changed this code:
$post->save();
to this :
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
It started to show an error which is:
FatalThrowableError in PostNot.php line 41: Call to undefined method
Illuminate\Notifications\Notification::send()
How can i fix this one please??
Thanks.
Instead of:
use Illuminate\Notifications\Notification;
you should use
use Notification;
Now you are using Illuminate\Notifications\Notification and it doesn't have send method and Notification facade uses Illuminate\Notifications\ChannelManager which has send method.
Using this
use Illuminate\Support\Facades\Notification;
instead of this
use Illuminate\Notifications\Notification;
solved the problem for me.
Hope this helps someone.
using this is better
use Notification
Instead of
use Illuminate\Support\Facades\Notification
this makes the send() not accessible [#Notification Databse]

Resources