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)
Related
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.
I added migrate in database, but why getting error I don't know, I am pretty new to Laravel, I don't know how can I fix this.
Error is:
Undefined variable: title
My code:
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$aboutus = new Abouts();
$aboutus->$title = $request->input('title');
$aboutus->$subtitle = $request->input('subtitle');
$aboutus->$description = $request->input('description');
$aboutus->save();
return redirect('/abouts')->with('success','nice');
}
}
you can write this way also.
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$input = $request->all();
Abouts::create($input); //here About us your model
return redirect('/abouts')->with('success','nice');
}
}
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);
}
After creating several Apps with Laravel and using softDelete properties I realized that methods like destroy(), restore() and kill() are exactly the same among several controllers. Therefore I am trying to put themn in a trait and use it from diferent Controllers.
My code is as follows:
ProfilesController.php
<?php
namespace App\Http\Controllers;
use App\Profile;
class ProfilesController extends Controller
{
public function destroy(Profile $profile)
{
Profile::del($profile, 'profiles');
return redirect()->route('profiles.index');
}
public function trashed()
{
Profile::trash('Profile');
}
}
Profile.php (model)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Profile extends Model
{
protected $fillable = ['user_id', 'role_id', 'title', 'subtitle', 'slug', 'birthday', 'about'];
use SoftDeletes, Helpers, commonMethods;
public function getRouteKeyName()
{
return 'slug';
}
// ... more code here
}
trait file: commonMethods.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use App\Profile;
use Session;
trait commonMethods
{
public static function del($element, $page_name)
{
$element->delete();
Session::flash('success', $element . ' successfully deleted!');
}
public static function trash($model)
{
$total = $model::onlyTrashed()->get();
$total_tr = count($total);
$all_tr = $model::all();
return view('partials.templates.trashed', compact('total', 'total_tr', 'all_tr'));
}
// ...more code here
}
The problem:
I try to visit the view "Trashed" that will list all elements "softdeleted" but not "killed", the method.
I pass the $model variable with the method trash($model)
I get the following error:
Class App/Profile does not found. Try to call App/Profile
I have debugged and the $model variable contains exactly what I need, the string 'Profile' which is what I need to build the Query:
$total = Profile::onlyTrashed()->get();
This query works while in the ProfilesController, but does not work while in a trait, since the model class is not found.
Any idea how could I make it work?
I am using Laravel 6.
If you need to use a class as a string you will want to use its full name. 'App\Profile' instead of 'Profile'.
$model = 'Profile';
new $model; // will use `\Profile`
$model = 'App\Profile';
new $model; // will use '\App\Profile';
In your controller( ProfilesController ) write :
use App\Profile;
In your model write :
use App\commonMethods;
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