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;
Related
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.
This is probably a pretty simple question. But in my Laravel project I have my User.php file which is located in
App\Models\User.php
And I need to call it in a file that is located in App\Http\Controllers.
So I reference it as
App\Models\User
but instead the code thinks I'm saying
App\Http\Controllers\App\Models\User
Which obviously is incorrect and causes and error. How do I reference a directory in Laravel that's outside the one I'm currently in?
You need to learn about how namespace works.
I suppose at the top of your controller file, there is a line
namespace App\Http\Controllers;
so If in your code you wrote
$c = new A\B\C()
it looks for App\Http\Controllers\A\B\C. You have two options:
use $c = new \A\B\C, in your case, \App\Models\User
add the namespace alias on the top under the namespace line:
use App\Models\User;
And then in your code $user = new User
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
sorry if it is a noob question, but I'm trying to learn laravel on laracast and can't solve this by my own.
there is a store function on my ArticlesController like this:
public function store(ArticleRequest $request)
{
$article = new Article($request->all());
Auth::user()->articles()->save($article);
return redirect('articles');
}
and it returns a blank page, making clear that is some error, but if I change to
\Auth::user()->articles()->save($article);
it works as expected, saving the article with the user_id field.
I tried import with use App\Http\Controllers\Auth\AuthController; but I think this is not the way.
*obs: Laravel 5.0
In modern PHP, if you see the following at the top of a file
namespace App\Foo\Bar;
it means all the code inside that file in part of the App\Foo\Bar namespace. If you try to use a namespaceless class inside this file
$object = new Auth;
PHP will assume you want to use the class. In other words, it's the same as saying
$object = \App\Foo\Bar\Auth;
When you say
\Auth
you're telling PHP "use the global, top level namespace class named Auth.
This is a perfectly valid use of PHP, by the way. However, if you don't like using the global \ -- you can import the \Auth class into your PHP file (sometimes referred to as a "module" in other languages") by using the use statement.
namespace App\Foo\Bar;
//pull in the global class `Auth`
use Auth;
//...
$foo = new Auth;
I created a package which also contains his own controller files and also a router.php file.
My problem is that i can't access anyone of the main classes like Schema or View.
I always get a Error: Class not Found error.
I'm assuming you're using namespaces in your package, which you should be. As a result, you need to precede global classes with a backslash to access them. For example, View::make() becomes \View::make().
Alternatively, you could import the Facades:
<?php
namespace Your\Namespace;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
//...
View::make($view, $data);
See the PHP namespace FAQ.