Getting "MongoDB\Driver\Manager Not Found" when using view composers in Laravel - php

I am using Laravel 7 and Jensseger's MongoDB package.
Everything works fine, except for this:
When I issue php artisan view:clear, I get MongoDB\Driver\Manager Not Found error.
I already have installed the latest version of MongoDB module for php 7.4 and everything else works fine. Meanwhile, I am using a dockerized environment. So, I am totally clueless why this is happening when I try to clear cached views.
This is my ViewServiceProvider file:
class ViewServiceProvider extends ServiceProvider
{
public function register()
{
}
public function boot(HeaderRepository $repository)
{
$header = $repository->getHeader();
View::composer('errors::*', function($view) use($header){
$view->with([
'header' => $header,
]);
});
}
}
My purpose is to create a customized 404 error page, but the header has to be fetched from MongoDB and shared between customized error pages. That's why I'm using view composers. Any other solution is welcome as well.

I suppose, you enter command php artisan view:clear from a local console.
You have choice:
Install mongo drive to your local interpreter
Use artisan from docker container eg docker exec {PHP_CONTAINER} php artisan view:clear

Related

Laravel/Passport - After remigration keys are not usable

Settings
Passport Version: "laravel/passport": "^9.3",
Laravel Version: "laravel/framework": "^7.0",
PHP Version: "php": "^7.2.5",
Database Driver & Version: MYSQL 8.0.15
Description:
I have used this article to create fresh API with laravel-passport. After installation I have successfully registered some users. Then after some coding I have needed to add some columns to existing tables. So after adding these columns I have used fresh command to remigrate all database.
php artisan migrate:fresh --seed
The operation was successfully and all tables(oauth tables also) have been created. But after this migration process existing passport grant tokens are not usable. When I register new User by sending normal AJAX request from web app interface I get this error.
Undefined property: stdClass::$refresh_token
I have tried many ways and researched about this issue but only after renewing old client and password keys process worked.
php artisan passport:install --force
Steps To Reproduce:
After installation passport just install and creating keys and migrate database. So everything will work. Then after remigrate database with command php artisan migrate:fresh . Then old keys will not be usable.
Even though this question is quite old. But we can use Event Listeners to achieve repeated steps.
Under app\Providers\EventServiceProvider.php add the following code in boot() method.
Laravel 9
public function boot()
{
parent::boot();
Event::listen(CommandFinished::class, function(CommandFinished $event) {
if($event->command === 'migrate:fresh') {
Artisan::call('passport:install', [ '--force' => true]);
echo Artisan::output();
}
});
}
I found the Illuminate\Database\Events\MigrationsEnded not working for my case. But other one, Illuminate\Console\Events\CommandFinished trigger on all command executions.
Do this to overwrite the existing keys and it will work
php artisan passport:install --force
Or you may publish Passport's configuration file using
php artisan vendor:publish --tag=passport-config
which will then provide the option to load the encryption keys from your environment variables:
Refer this for detailed instruction https://laravel.com/docs/7.x/passport

Laravel console command trait method not found

I have a Laravel project that has been deployed with Forge and had OPCache enabled using Forge. I noticed last week that when I pushed some changes, the changes that were in the views and in the controllers were present on the server, but custom artisan commands that I run don't recognize updates.
Put another way, updates to the blades are showing on the screen. Updates that I have added to the controllers are changing the way information is passed to the blade files, but I have a custom artisan command that runs a series of methods in a trait. The actual file on the server shows the new method that I pushed, but when I run the artisan command in the CLI, it says that the method cannot be found.
I have stopped, restarted, and reloaded OPCache countless times. I have restarted Nginx. I have disabled OPCache and restarted PHP. It is still saying that the method is not found. Does anyone have any ideas?
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Traits\FTPImportsTrait;
class CheckFTPImports extends Command
{
use FTPImportsTrait;
protected $signature = 'checkForImports';
protected $description = 'Check for imports...';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->checkBankImports();
}
}
-----------
<?php
namespace App\Traits;
trait FTPImportsTrait;
{
public function checkBankImports()
{
dd('YOU ARE NOT CRAZY');
}
}
$ php artisan checkForImports
$ method checkBankImports does not exist.
UPDATE:
It has to be some sort of configuration issue on the server. I just deployed the project to a fresh DO droplet and the command works as expected.
It only happened in the production environment for me.
Running:
php artisan clear-compiled
deleted the cached version and solved my issue.
Thanks a ton to #num8er.

Class 'Datatables' not found Laravel 5.4

I am using laravel 5.4 and php version is 5.6.
I am getting error "Class 'Datatables' not found". I am following standard procedure. I have installed datatable using composer by following command:
composer require yajra/laravel-datatables-oracle:"~7.0"
I added these two lines in config->app file in service provider and aliases:
Yajra\DataTables\DataTablesServiceProvider::class,
'DataTables' => Yajra\DataTables\Facades\DataTables::class,
then publish package :
php artisan vendor:publish --provider=Yajra\DataTables\DataTablesServiceProvider
I am using raw query here is my controller code :
function allvendor(){
$sql="my query";
$results=DB::table(DB::raw("($sql)"));
return Datatables::of($results)->make(true);
}
It's a typo. You set alias as DataTables but returned is Datatables.

laravel on windows cant find views

Hello i installed laravel 4.2 on my windows pc and get error that view cannot by foundet
InvalidArgumentException
View [layout.index] not found.
my controller content
protected $layout = 'layout.index';
public function home()
{
return $this->layout->content = View::make('content_index.home');
}
but realy file exists 'views/layout/index.blade.php'
its kinda bug or something ?
The folder is actually /layouts, so your view should be located at :
protected $layout = 'layouts.index';
my problem is start after I run this on local:
php artisan config:cache
after that I run this in vagrant and solve problem.
open ssh
php artisan config:cache

Laravel running migrations on "app/database/migrations" folder recursively

So my migrations folder looks like this since I have dozens of tables it keeps things organized and clean:
migrations/
create_user_table.php
relations/
translations/
I'm trying to do a refresh all migrations and seed but it seems like I've run into a slight hiccup where I don't know the artisan command to run migrations recursively (i.e. run migrations in the relations and translations folders as well).
I've tried adding --path="app/database/migrations/*" however it spat out an error. Does anyone know the solution to this?
The only way to do it right now is to manually go through all the migrations. That is, you have to run the migration command on each of your subfolders:
php artisan migrate --path=/app/database/migrations/relations
php artisan migrate --path=/app/database/migrations/translations
However, what you can do is easily extend the artisan system to write your own migrate command that will iterate through all folders under the migrations folder, create these commands for you and run them.
You can also simply write a shell script if you don't want to get into doing this via artisan
Edit: for Laravel >= 5.0, the correct commands to migrate migration files in sub directories would be:
php artisan migrate --path=/database/migrations/relations
php artisan migrate --path=/database/migrations/translations
This add to boot method in AppServiceProvider
$mainPath = database_path('migrations');
$directories = glob($mainPath . '/*' , GLOB_ONLYDIR);
$paths = array_merge([$mainPath], $directories);
$this->loadMigrationsFrom($paths);
Now you use can php artisan migrate and also php artisan migrate:back
You can also use a wildcard, like so:
php artisan migrate --path=/database/migrations/*
You can use the following command to do this recursively:
php artisan migrate --path=/database/migrations/**/*
**/* is also known as the globstar
Before this works you must check if your bash supports the globstar.
You can do this by executing shopt and checking for globstar.
Globstar is supported by default by most server distributions but might not work on MAC.
For more on globstar see: https://www.linuxjournal.com/content/globstar-new-bash-globbing-option
In Laravel 5 the database folder sits alongside the app folder by default. So you could run this to migrate a single folders migrations:
php artisan migrate --path=/database/migrations/users
You can try this package nscreed/laravel-migration-paths. By default all sub directories inside the migrations folder will be autoloaded. Even you can add any directories very easily.
'paths' => [
database_path('migrations'),
'path/to/custom_migrations', // Your Custom Migration Directory
],
Don't need any special command just the generic: php artisan migrate will do your tasks.
I rewrote the MigrationServiceProvider:
- registerResetCommand()
- registerStatusCommand()
- registerMigrateCommand()
There you can register your own commands:
class MigrateCommand extends Illuminate\Database\Console\Migrations\MigrateCommand
After that you just need to extend youd directories:
protected function getMigrationPaths()
Or you just register the paths on application boot.
I've already done my solution before I knwewd about '$this->loadMigrationsFrom'.
A Simple solution is to create an Artisan Command for example (migrate:all),
then inside handle function define migrate command for each sub directories as mentioned bellow.
Artisan::call('migrate', [
'--path' => '/database/migrations/employee'
]);
Only relative path works for me (in Laravel 5.7):
php artisan migrate --path=database/migrations/your-folder-with-migrations
This works at laravel 8
php artisan migrate --path=database/migrations/tenant
It is a not a "direct" solution but i suggest you to look at Modularity into your laravel project.
Modules can segment your application in several smaller "folders" and bring migration, seeds, classes, routes, controllers, commands together in easily maintainable folders.
This package is a good start : https://github.com/pingpong-labs/modules
No changes are necessary, whenever you need it, use this command:
php artisan migrate --path=database/migrations/*
If you want a shortcut, you can include that in your composer.json:
"scripts": {
"migrate": [
"#php artisan migrate --force --path=database/migrations/*"
]
}
Then just use composer migrate in the terminal.
add in app/Providers/AppServiceProvider.php the path of your migration folder, you can add a string or an array
public function register()
{
$this->registerMigrations();
}
protected function registerMigrations()
{
return $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations/**/*');
}
A Simple Laravel solution is to create a gulp task (say migrate-others).
var elixir = require('laravel-elixir');
var gulp = require('gulp');
var shell = require('gulp-shell')
/*
|--------------------------------------------------------------------------
| Elixir Asset Management
|--------------------------------------------------------------------------
|
| Elixir provides a clean, fluent API for defining some basic Gulp tasks
| for your Laravel application. By default, we are compiling the Sass
| file for our application, as well as publishing vendor resources.
|
*/
elixir(function(mix) {
mix.sass('app.scss');
});
// Our Task
gulp.task('migrate-others', shell.task([
'php artisan migrate --path=/app/database/migrations/relations',
'php artisan migrate --path=/app/database/migrations/translations',
]));
Now you can simply call
gulp migrate-others
Here you go!
function rei($folder)
{
$iterator = new DirectoryIterator($folder);
system("php artisan migrate --path=" . $folder);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
echo $fileinfo->getFilename() . "\n";
rei($folder . $fileinfo->getFilename() . '/');
}
}
}
rei('./database/');

Categories