Storage connection refused Laravel

Sometimes, I got this error message. The console said that the connection is refused to my storage folder. I stuck in this problem for a while. Check all the permission, symbolic link, etc. But it still there. Finally, the problem is solved by a simple line of code.

I forgot to update the APP_URL in the .env file.

// APP_URL=http://d.vietartisans.io:1991
APP_URL=http://d.vietartisans.io:91

Could not install or update the theme, plugin in WordPress

Sometimes, you get this error message.

To perform the requested action, WordPress needs to access your web server. Please enter your FTP credentials to proceed. If you do not remember your credentials, you should contact your web host.

That means you are a user who has no permission to write the site source code. To update this you can use this command in the terminal if you want to writable whole site.

chown -Rf www-data.www-data /var/www/html/your-site-folder

You also can specify the themes or plugins which you want to writable.

chown -Rf www-data.www-data /var/www/html/your-site-folder/wp-content/themes or plugins

Get count of eager loading relationship in Laravel

This is how I do it

        $user = $this->users->get(auth()->user()->id);
        $user->loadCount('collections');
        $collectionsCount = $user->collections_count;

        $membersCount = 0;
        tap($user->joinedAsManagerCollections()->get(), function ($collections) use (&$membersCount) {
            $collections->each(function ($collection) use (&$membersCount) {
                $collection->loadCount('members');
                $membersCount += $collection->members_count;
            });
        });

How to specify the ID for ElSelect

I’ve recently used https://element.eleme.io which UI Framework for Vuejs. I got a situation when I was using the ElSelect component that I need passing an object to the value attribute of ElOption component. This is my first init code for this.

            <el-select
              class="xs:w-full"
              v-model="selectedSiblings"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="Choose sibling for your club">
              <el-option
                v-for="item in siblings"
                :key="item.id"
                :label="`${item.first_name} ${item.last_name}`"
                :value="item">
              </el-option>
            </el-select>

I used the id value for the key attribute. But it did not work. Vuejs showed me an error message said that the ElOption component could not specify the key.

I researched around on Google for a while and found the answer. The answer is we need to set another attribute for ElSelect component to determined the key in this case. It is value-key attribute. So I added it to the ElSelect component and it worked.

            <el-select
              value-key="id"
              class="xs:w-full"
              v-model="selectedSiblings"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="Choose sibling for your club">
              <el-option
                v-for="item in siblings"
                :key="item.id"
                :label="`${item.first_name} ${item.last_name}`"
                :value="item">
              </el-option>
            </el-select>

Hope this help.

Issue installing Carbon package in Laravel 5.8

Today, I got this error while I’m trying to install Laravel Dusk.

Error message with Carbon package

The error message said that we need to use the new version of Carbon instead of current version 2.18.0. Since Laravel has already used the Carbon as default but it used 2.1*. So I alias the current version as the latest version in composer.json.

    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.8.*",
        "laravel/tinker": "^1.0",
        "nesbot/carbon": "2.18.0 as 1.34.0"
    },

Finally, I ran the command install the Laravel Dusk again. The error has gone.

The installation Laravel Dusk successfully

The server requested authentication method unknown to the client in MySQL 8

Hello guys,
Today, I got another error when I tried to run migrate artisan command in Laravel. The error said: “The server requested authentication method unknown to the client”. This error appeared after I upgraded MySQL server from 5.7 to latest version as known 8.0.

By default, for some reasons, the default plugin of MySQL 8 is auth_socket. So the Applications will most times expect to log in to your database using a password. If you have not changed your mysql default authentication plugin yet, you can do these:

I have to change MySQL configuration. I edited the my.cnf file and put this line right below the [mysqld] like this.

[mysqld]
default_authentication_plugin= mysql_native_password

Then, I logged in the MySQL as root. Next, I ran these commands in the terminal.

ALTER USER 'admin' IDENTIFIED WITH mysql_native_password BY 'secret';
FLUSH PRIVILEGES;

Replace “secret” with your root password. In case your application does not log in to your database with the root user, replace the “admin” user in the above command with the user that your application uses.

Route Order and Overwrite defined in Package in Laravel

I faced a problem that I have a route which does not work in any case. It always show not found.

After researching around, I know that because of the ordered of the routes. To demonstrate, imagine I have 2 routes:

GET /post/{id} and GET /post/create

So in this case, the second route will be never called. Because Laravel matches the first route and immediately break at this point.

Back to my problem, I comment our all my routes except the problemable route. But it still does not work. I list all routes in the application by php artisan route:list command. And below is my result.

There is another route which defined by Laravel Spark package. It overwrites all my route because it called very first by the application. So I have to find out either call my routes before that route called or overwrite it.

I googled and found this answer https://stackoverflow.com/a/44724330/4184355. The answer said we can overwrite it by rearrange the Service Provider which define in config/app.php. I write RouteServiceProvider before SparkServiceProvider . I access my route again and get this error The bootstrap/cache directory must be present and writable. It means I have to clear the cache. So I run php artisan cache:clear and the problem is gone. My route works again.