Laravel8: Class not found despite defining it in the models - php

I used the model "clients.php" but when I load in the ClientsController
$clients= Client::all()->sortby('name');
I get the: Class "App\Http\Controllers\Admin\Client" not found
What would be the solution? I knew that if I create a "clients" model, then it should load as "client" in my function

You should use full namespace, for example (if Client model is stored inside Models directory):
$clients = \App\Models\Client::all()->sortby('name');
or you can add use App\Models\Client; at the beginning of the file after the namespace declaration and then just use
$clients = Client::all()->sortby('name');

You have to import the Client model. If you use Laravel 8 then :
use App\Models\Client;
$clients = Client::all()->sortby('name');
OR
If you use Laravel 7 or below then:
use App\Client;
$clients = Client::all()->sortby('name');

you can do .
use your client controller
use App\Models\Client;
then
$clients= Client::all()->sortBy('name');
OR
call direct model in your controller.
$clients = \App\Models\Client::all()->sortBy('name');

If you provided the namespace in the Controller and it's still not working,
open the terminal and type "composer dump-autoload". I do it every time i have a new Model.

Related

How to use Laravel models in ordinary PHP files

I have a Laravel website; within this I have an ordinary PHP file which responds to a webhook.
How can I use Laravel models (eg \App\User) in this PHP file?
Thanks
The same way you use with blade. But just, you can't use blate curly braces {{}}. So you will have to echo everythig. If you pass a $user from the controller, then you can do
<?php echo $user->email; ?>
If you want to access models directly from the file without passing from another file then give it a namespace and make use of use.
myfile.php
<?php
namespace App\Wherever\You\Want;
use App\User;
...
You can use the model in your php file like this.
use App\Models\User;
and call the methods of the model by creating instance of this model.
$user = new User;

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

Phalcon PHP multi module namespace definition

I'm using Phalcon PHP with Multi module application. I'm using namespace in my project but I'm searching for something to use theses namespace.
For example, in my view folder I'm using the models folder and in my controller I use the models folder too. But I'm using lot of class models to do a Phalcon find or findFirst. And the only way than I found to make this multi apps working, it's to define the namespace used to import the class like this :
use Apps\Common\Models\Users;
use Apps\Common\Models\Customers;
use Apps\Common\Models\Agents;
...
And I have 50 models like this in my apps... I don't want to define them in all my controller and all my view to make it work.
Do you have a solutions for that ?
Thanks.
If I understood correctly, you can omit the namespace declaration on top of your controller file:
use Models\News;
class NewsController extends BaseController
{
public function indexAction()
{
// With Use above
$obj = new News();
// Without Use above (full namespace path)
$obj = new \Models\News();
}
}

Yii 2 Namespace missing?

I have a namespace missing problem in Yii 2. I installed the advanced application. I am referencing a backend model from my frontend controller. Below is a code snippet of my backend model, frontend controller and error message.
Error
Unable to find 'backend\models\PaymentsMethod\TermsAndConditions' in file: C:\inetpub\wwwroot\jobmanager/backend/models/PaymentsMethod/TermsAndConditions.php. Namespace missing?
Backend Model
namespace app\models\PaymentsMethod;
use Yii;
class TermsAndConditions extends \yii\db\ActiveRecord
{
Frontend Model
public function actionCreate()
{
$model = new estimate();
$tnc = new \backend\models\PaymentsMethod\TermsAndConditions();
I have resolved my problem. I was trying to access a backend model class from a frontend controller. I resolved this by moving the backend model class to the common folder and from there I can reference it from both the backend and frontend.
Thanks
In your frontend, first include the namespace and then instantiate:
use app\models\PaymentsMethod\TermsAndConditions;
$tnc = new TermsAndConditions();
OR
As alfallouji said you can directly use:
$tnc = new \app\models\PaymentsMethod\TermsAndConditions();
If you are accessing from frontend then use frontend instead of app
i.e
namespace frontend\models\PaymentsMethod;
and if you are accessing from backend then use as below
namespace backend\models\PaymentsMethod;
You defined the model using this namespace app\models\PaymentsMethod and then you are trying to instantiate \backend\models\PaymentsMethod\TermsAndConditions.
You should be doing that in your frontend model :
$tnc = new \app\models\PaymentsMethod\TermsAndConditions();
Namespace declaration statement has to be the very first statement in the script
123456789101112
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;
class UserController extends Controller{
public function actionIndex()
{
echo "working on .....";
replace "backend" for "app" only models search
ex: app\models\PaymentsMethod;

Can I use DB::table('tags')... outside of Laravel?

I'm using Eloquent ORM in my Zend application. This allows me to do things like:
$capsule = new Capsule();
$capsule->addConnection( $config );
$capsule->bootEloquent();
.
.
.
$question = new Question();
$question->user_id = $user->id;
$question->title = $params->title;
$question->content = $params->content;
$question->save();
However, I want to do the following to allow me to perform multiple inserts:
DB::table('tags')->insert($values);
..but ofcourse I get the following error:
Fatal error: Class 'DB' not found ...
Can I use these DB::* methods outside of Laravel? I've had a look in my /vendor/Illuminate/Database directory but I don't really know which class it's referring to.
First you would need to refer to the namespace where "Capsule" class is located in. At the top of the script add:
<?php use Illuminate\Database\Capsule\Manager as Capsule;
Then you can call:
Capsule::table('part')->where('id', '=', '1')->get();
Similarly in your case try using:
Capsule::table('tags')->insert($values);
DB is refferring to Illuminate\Database\DatabaseManager so if you add at top of your file:
use Illuminate\Database\DatabaseManager as DB;
you should be able to use DB
If you're in a namespace other than Illuminate simply you can use it as \DB it will look outside of your namespace and will find the DB Facade
Actually, this worked:
Tag::insert($values);
Don't know why I didn't think to try this. Thanks anyway.

Categories