Auth not working in Laravel Tinker - laravel

From within Tinker I am trying to:
>>> Auth::loginUsingId(12);
=> false
>>> Auth::user();
=> null
>>> Auth::attempt(['email' => 'my#email.com']);
=> false
I am guessing that since Auth typically uses session data, and maybe sessions don't work with tinker.
Is it possible to authenticate within tinker?

It's possible to login in Tinker. For example:
auth()->loginUsingId(1)
auth()->id()
Normally, the output of the auth()->id() will be 1.
If it doesn't work for you, make sure the storage directory is writable:
sudo chmod -R 755 storage
You're also doing it wrong when you're using the attempt() method. The correct syntax is:
attempt(['email' => 'my#email.com', 'password' => 'secret'])

Related

Why is the array empty on get allFiles with Laravel storage

I have an Laravel application witch works with an ftp server. Till now, I used the ftp service from hostpoint.ch. Everything works well.
Now, the customer like to use his own ftp server. So I created a new disk for the new ftp.
'ftp_customer' => [
'driver' => 'ftp',
'host' => 'xxx.xxx.xx.x',
'username' => 'xxx',
'password' => 'xxx',
'port' => 21,
'passive' => true,
'root'=> '/',
],
To test the server, I created the following script:
Storage::disk('ftp_customer')->put('file1.txt', 'Contents');
$files = Storage::disk('ftp_customer')->allFiles();
dd($files);
On the server, the file1.txt exists:
-rw-rw-rw- 1 ftp ftp 8 Dec 20 13:43 file1.txt
Therefore, I know, that the put method works.
But the get allFiles returns always an empty array.
I tried to create first a folder and use it. Also, I tried to change the config.
I'm not sure if it creates problems, because it's a Windows server.
Does anyone have an idea how to solve it?

Laravel 9 Storage::disk()->exists() works on local but not on AWS S3

When I save files in local Storage on my Mac, and check if a file exits and delete it, it works fine.
>>> $path = "public/documents/47/mCzPh9USfoCnsRdt6xZcSsvY1YAzIRu3HuUChPpJ.pdf";
=> "public/documents/47/mCzPh9USfoCnsRdt6xZcSsvY1YAzIRu3HuUChPpJ.pdf"
>>> Storage::disk('local')->exists( $path);
=> true
>>>
>>> Storage::delete($path);
=> true
>>>
However, when I use exists() on AWS s3, it always returns false, even when the file exists (I can see the file in AWS console and can access it (download it) through my website or delete it. Here is the output from my tinker session
>>> use Illuminate\Support\Facades\Storage;
>>> $path = 'client/documents/178/gfPAVrp2vBnKAAQ5xQ6NL4HvG9RQN3cJ0PtzBFNw.jpg';
=> "client/documents/178/gfPAVrp2vBnKAAQ5xQ6NL4HvG9RQN3cJ0PtzBFNw.jpg"
>>> Storage::disk('s3')->exists($path);
=> false
>>> Storage::disk('s3')->exists('client/documents/178');
=> true
>>> Storage::disk('s3')->delete($path);
=> true
Notice that exists() is able to confirm that the folder exists and delete() works.
Here is the key for my file from AWS console:
client/documents/178/gfPAVrp2vBnKAAQ5xQ6NL4HvG9RQN3cJ0PtzBFNw.jpg
Am I doing anything wrong? Is there any reason for the Storage exists() to fail on AWS S3?
Thanks.

Mews\Purifier\Purifier::__construct() must be an instance of Illuminate\Filesystem\Filesystem

I installed Mews-Purifier on laravel 5.1
I used Froala Editor.
https://stackoverflow.com/
<script>asdfasdfasdf</script>
<iframe>qwerqwerqwer</iframe>
I expect result.
<p>https://stackoverflow.com/</p>
<p>qwerqwerqwer</p>
but, I am not see it.
when save it,
$data['detail'] = app('purifier')->clean($data['detail']);
if use Purifier::clean($data['detail']), It send error message me.
Non-static method Mews\Purifier\Purifier::clean() should not be called statically
so, It changed.
$data['detail'] = (new \Mews\Purifier\Purifier)->clean($data['detail']);
send error message too.
Type error: Too few arguments to function Mews\Purifier\Purifier::__construct(),
so I do changed.
(new Mews\Purifier\Purifier($data['detail'], ['AutoFormat.DisplayLinkURI' => true] ))->clean($data['detail');
Type error: Argument 1 passed to Mews\Purifier\Purifier::__construct() must be an instance of Illuminate\Filesystem\Filesystem, string given, called in
what Filesystem? what file??
Let me know How should I use it?
If I wasn't good at installing, what would it be?
Oh the installation did the following.
terminal
composer require mews/purifier
/config/app.php
'providers' => [
// ...
Mews\Purifier\PurifierServiceProvider::class,
],
'aliases' => [
// ...
'Purifier' => Mews\Purifier\Facades\Purifier::class,
]
terminal
php artisan vendor:publish
check created file. /config/purifier.php
Thanks for watching
Please let me know if you know.

Error: Unsupported DB driver for windows and wamp

I already config my .env file and db.php file for craft with the same information, before i used 'mysql' as driver but i try it as empty and throws the same error.
db.php
<?php
/**
* Database Configuration
*
* All of your system's database connection settings go in here. You can see a
* list of the available settings in vendor/craftcms/cms/src/config/DbConfig.php.
*
* #see craft\config\DbConfig
*/
return [
'driver' => getenv(''),
'server' => getenv('localhost'),
'user' => getenv('root'),
'password' => getenv('****'),
'database' => getenv('craftyblog'),
'schema' => getenv(''),
'tablePrefix' => getenv(''),
'port' => getenv('')
];
.env
# The environment Craft is currently running in ('dev', 'staging', 'production', etc.)
ENVIRONMENT="dev"
# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY="******"
# The database driver that will be used ('mysql' or 'pgsql')
DB_DRIVER=""
# The database server name or IP address (usually this is 'localhost' or '127.0.0.1')
DB_SERVER="localhost"
# The database username to connect with
DB_USER="root"
# The database password to connect with
DB_PASSWORD="****"
# The name of the database to select
DB_DATABASE="craftyblog"
# The database schema that will be used (PostgreSQL only)
DB_SCHEMA=""
# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)
DB_TABLE_PREFIX=""
# The port to connect to the database with. Will default to 5432 for PostgreSQL and 3306 for MySQL.
DB_PORT=""
DEFAULT_SITE_URL=""
And i'm using WAMP with this versions:
PHP 7.1.16
Apache 2.4.33
MySQL 5.7.21
I expect solve the problem, thank you.
In your config.php, it appears you're trying to pull in environment variables via getenv(), but you're passing along the actual values you want to use as strings to the getenv() function instead of the environment variable name. The values are set in the .env file so it's more portable for collaborative developers.
In that .env file, there isn't have an environment variable set for the database driver, so you can just pass a string to 'driver' instead in config.php.
If you'd like to pull the values from your .env file, pass the variable names as strings for the environment variables in the expected format for getenv(), like so:
config.php
return [
'driver' => 'mysql',
'server' => getenv('DB_SERVER'),
'user' => getenv('DB_USER'),
'password' => getenv('DB_PASSWORD'),
'database' => getenv('DB_DATABASE'),
'schema' => getenv('DB_SCHEMA'),
'tablePrefix' => getenv('DB_TABLE_PREFIX'),
'port' => getenv('DB_PORT')
];
Your .env file already seems setup for everything, so you should be good to go. However, to use the values from the .env file in the config.php file, you're going to need to pass the variable names as strings. Hope this helps!

Laravel change filesystem disks path on run time

I am aware of the filesystems.php to create disks and I'm currently using it, having ~~ 20 disks configured.
I have a new problem with these, I'm currently trying to prefix to every disk, a string. The problem is that the paths are being saved when the php artisan config:cache is run but I need to change them on run time, as n example, for User Sergio it would need to append Sergio/ to the following disk for example:
//filesystems.php
'random' => [
'driver' => 'local',
'root' => storage_path('app/random'),
],
Then
Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
//outputs /var/www/html/project/storage/app/random
and the goal is setting configurations in for example the middleware i'm currently setting the tentant database already like this
//Middleware
Config::set('database.connections.tenant.database', "Sergio");
DB::reconnect('tenant');
I can currently set the paths correctly with
Config::set('filesystems.disks.random.root',storage_path('app/Sergio/random'));
But i'm worried since that if before that line I try to reach to the path, the storage saves the initial path in memory instead of re-fetching it after it is altered.
For example. doing this without any middleware.
$fullPath1 = Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
Config::set('filesystems.disks.random.root',storage_path('app/Sergio/random'));
$fullPath2 = Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
What was intended to happen is that $fullPath1 would output the initial path which is /var/www/html/project/storage/app/random and then $fullPath2 would output /var/www/html/project/storage/app/Sergio/random
Is there any way of letting the Storage know that I've changed the disks local paths?
How about adding a new config instead of updating the already loaded one, something like this:
private function addNewDisk(string $diskName)
{
config(['filesystems.disk.' . $diskName => [
'driver' => 'local',
'root' => storage_path('app/' . $diskName),
]]);
}
and prior to using the Storage facade, call above method that way the config will be updated and when you use new disk, it will try to resolve again based on updated config.
{
....
$this->addNewDisk('new_random');
Storage::disk('new_random')->get('abc.txt'); // or any another method
...
}

Resources