Importing class in laravel 5.2 - php

use App\User;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
do i need to write the above import class every time when i create a new controller?In laravel 4.2 it does automatically
is any other way to do this?

Yes, you do. Your app is under the namespace App;, so if you want to use the Input class, but you don't say use Illuminate\Support\Facades\Input, PHP will try looking for it under your namespace App; and an exception will be thrown since the class you're looking for probably won't be there.

Those are not a mandatory class.
When ever you are using those facedes you have to add
There is one way i know.
class something like CustomController
Add the all the common classed in that controller.
Now for every controller you can extend the new CustomController

Related

Laravel 7 package controllers parent class

I have developed a package containing package controllers. Everything is loading and working fine, but currently the package controller looks like this:
<?php
namespace MyPackage\App\Http\Controllers;
use App\Http\Controllers\Controller; // Stock Laravel controller class
use Illuminate\Http\Request;
use Voice\CustomFields\App\Form;
class FormController extends Controller
{
...
}
I want the class to extend Laravel Controller class, however inside my package this class of course doesn't exist as it is a part of a standard Laravel app.
I ended up including use App\Http\Controllers\Controller;, but I don't see it as a good practice since I'm referencing something that doesn't exist up to the point I include my package in the app.
How can I do this the right way? I can replicate the class within my package, but this would be duplicating logic + additional problems if tomorrow a new Laravel version is released with a modified Controller class.
Just look in source code of App\Http\Controllers\Controller
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
You can extend BaseController in your package controllers. If Laravel team change something, it will be written in migration instructions.
If you need some specyfic logic in your package BaseController and the same logic in app BaseController, consider using trait or ServiceProvider.
Your controller doesn't necessarily need to extend the default "base controller". Usually packages either extend the Illuminate\Routing\Controller directly or have their own base controller to extend all the other controllers with.
If you are using the stock base controller without any custom code added, there won't be any duplicate lines if you choose to copy it to your package as it only consists of three optional traits providing helper functions wrapping more complicated code.

How to fix Class 'App\Http\Controllers\Notification' not found in laravel?

I have a Controller which listens to a new Schedule creation and sends the result back to the view via ajax. Inside of it I want to add a Notification to send email to the user once the Schedule cannot be completed due to a lack of resources at that specific date and time.
The problem is that I get the error below:
Class 'App\Http\Controllers\Notification' not found in /laravel/app/Http/Controllers/DadosAgendamentoController.php on line 89
The folder structure is this:
-app
-Http
-Controllers
DadosAgendamentoController.php
-Notifications
AgendamentoPendente.php
DadosAgendamentoController.php head code:
namespace App\Http\Controllers;
use Input;
use Request;
use App\Servicos;
use App\Disponibilidades;
use App\Estabelecimentos;
use App\HorariosEstabelecimento;
use App\Agendamento;
use App\User;
use App\Notifications\AgendamentoPendente;
line 88 and 89:
$user = User::where('id',1)->get();
Notification::send($user, new AgendamentoPendente(1));
Trough my Controller I can access all the classes above, but not the AgendamentoPendente
My goal is to send an email do the admin so he can suggest a new date and time to the client when the resources are not available at the desired date and time.
How can it be fixed? Can I access the class in this Controller? How?
Notifications may be sent in two ways: using the notify method of the Notifiable trait or using the Notification facade.
https://laravel.com/docs/5.3/notifications#sending-notifications
Option 1
You can use notify() method:
$user->notify(new AgendamentoPendente(1));
Also, make sure User class uses Notifiable trait:
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
Option 2
Using facade with full namespace:
\Notification::send($user, new AgendamentoPendente(1));
Add use Notification; in your controller
OR
alternatively, use \Notification::send($user, new AgendamentoPendente(1));
add this at the top of the controller:
use App\Notifications\AgendamentoPendente;
i was having the same problem and this fixed it
Also note that if you are using the facade, make sure your User queries the email field from your database
$users = User::select("email")->get();
\Notification::send($users, new AgendamentoPendente(1));
You have to use facades at the top
use Illuminate\Support\Facades\Notification;
You can refer to this tutorial
https://thecodingsolution.com/view/laravel-notofication-laravel-database-notification
You can pull in the notification library used Lumen 8.0:
"illuminate/notifications": "5.3.*"
into your composer.json then running composer update to pull in the notification libraries.
You will also need to add
$app->register(Illuminate\Notifications NotificationServiceProvider::class);
to your bootstrap/app.php
This process working for me.
Thanks

How to use Laravels spatie/laravel analytics package

I just downloaded this package for Laravel.
spatie/laravel-analytics
Its a Google Anayltics package, and I followed all the steps for setting up an account. What I'm having trouble is calling the methods. For example when it says:
Here is an example to retrieve visitors and pageview data for the current day and the last seven days.
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
I tried doing this in my function like this:
<?php
namespace App\Http\Controllers\Admin;
use Carbon\Carbon;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Spatie\Analytics\Analytics;
use Illuminate\Support\Collection;
class DashboardController extends Controller {
public function index() {
$analytics = Analytics::fetchVisitorsAndPageViews(Period::days(7));
dd($analytics);
return view('admin.dashboard-v2');
}
}
Its giving me errors like:
Non-static method Spatie\Analytics\Analytics::fetchVisitorsAndPageViews() should not be called statically
Am I missing something here? I couldn't find any specific documentation online except for the Github ReadMe file
If you want to use the facade to access the class, you'll need to change use Spatie\Analytics\Analytics; to use Analytics;. That should take care of that error.
If you are going to use Period::days(7) then you will need to add use Spatie\Analytics\Period; because that's an actual static method, not a facade.

Laravel - Class/Model not found

Initially, this code worked, but it doesn't work anymore. I don't know what is causing the issue.
The error is :
FatalErrorException in AdminController.php line 64:
Class 'App\Category' not found
AdminController code is:
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Category;
use View;
class AdminController extends Controller
{
public function article_get()
{
$categories = Category::latest()->get();
return View::make('create.article')->with('categories',$categories);
}
}
My model Category is located at App/Models/Category.php.
What I've tried:
Change from use App\Category; to use Category, to use \Category, to use App\Models\Category.
composer dump-autoload.
A few hours ago I had a working project, but now my models are not found.
Because Laravel uses PSR-4 autoloading, you need to make sure your namespaces match the real directory structure.
You say that your Category model is located at App/Models/Category.php so its namespace should be App\Models. Then, in your controllers you would use App\Models\Category.
I had also faced the same error when Models are called inside Controllers or Seeders.
The best solution is to import your Model inside the Controllers.
Add the below line at the top of your AdminController.
use App\Models\Category
This is applicable for all Classes where you try to call your models.
Do it like this and it must work:
use App\Models\Category
and make sure that the model Category is inside a folder named Models.
Your code seems fine.
May be it has an issue with namespace in Category model.
if you use this code for Category Model in your AdminController controller:
use App\Models\Category;
then your Category model itself has this namespace
namespace App\Models;
Check if your model namespace is still there or not.
Thanks
App\Models\Category
PLease Check This twoThings
1.Importing namespace App\Http\Controllers;
2,Check Controller Letter is in Upper Or LowerCase
like this
use App\models\Category----->❌❌❌
use App\Models\Category----->✅✅✅
use App\Models\Category
//check your models is capital or small letter sometimes issue like that is a big problem

performance and the use statment PHP, Laravel 4.x

Are there performance considerations when importing classes with the use statement in PHP and more specifically laravel 4.x ?
for example often times i have many use statements in my controllers as such:
use OrganisationController;
use Input;
use Redirect;
use Validator;
use View;
use Organisation;
use Sentry;
use User;
use Str;
use Lang;
use Application;
use Job;
use Upload;
class ApplicationController extends OrganisationController {
...
In my opinion, it should not effect on performance because, when you use a class that is not in the same namespace you use it with a preceding backslash like
\View::make(...);
This way you just telling that, the View class is within the global namespace, to make the code clean you can use use statement top of your class like
use \View, \Redirect, \Validator; // more
class WhatEver extends BaseController {
function index()
{
//...
return View::make(...);
}
}
So, in either way, php is loading (include/require) the class whenever it founds any use of the class but when it's not in the scope of current namespace you must provide the namespace and it could be directly from the code like
\View::make(...);
Or maybe using a use statement to keep your code clean but in both ways, php is loading the class during run time (if not already loaded) but you have to provide the right location (with namespace).

Categories