Monday, March 11, 2024

RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)

When I was trying to do a git clone, I got the below error:


The error was RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 0)
some bytes of body are still expected. Unexpected disconnect while reading sideband packet. Early EOF. 
I have gone through google to solve this issue. I found different solutions. Some of them even told it's due to network. You need to change the internet operator etc. But all in vain. The correct solution is , you need to increase the buffer size of http post with the below command

git config --global http.postBuffer 157286400
After this I was able to clone the repo properly.

Happy coding

Friday, February 2, 2024

Seeding: Array[ReflectionException]Class Array does not exist Error while trying to seed the database in Laravel

While I tried to seed the database in laravel after the migration with the below artisan command,

php artisan db:seed

I received the below error: 
Seeding: Array
[ReflectionException]
Class Array does not exist

There are few possible causes for the error message “ReflectionException: 
Class Array does not exist” that appears when Laravel tries to use class that it cannot locate. These include: The class might not have been properly imported or required at the top of the file. The class might not have been properly imported or required at the top of the file. Composer dump-autoload may not be done.

I solved this by running the below command:

composer update

and then run:

composer dump-autoload


Now run

php artisan db:seed

It works. Happy coding

Tuesday, January 9, 2024

Laravel JWT AUTH only returns TRUE/FALSE instead of the token

When using Laravel JWT authentication, the default behavior is to return a boolean (`true` or `false`) indicating whether the authentication was successful or not. If you want to receive the token upon successful authentication, you might need to customize the authentication response.

Here's how you can modify Laravel to return the token upon successful authentication using JWT:

1. Install the JWT package:

   Make sure you have the `tymon/jwt-auth` package installed. You can install it using Composer:


   composer require tymon/jwt-auth
  

2. Publish the configuration file:

   Run the following command to publish the JWT configuration file:

   
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
  

3. Configure the `config/jwt.php` file:

   Open the `config/jwt.php` file and set the `'return_user_token'` configuration option to `true`:

      /*
   |--------------------------------------------------------------------------
   | Return User Token
   |--------------------------------------------------------------------------
   |
   | If set to true, the middleware will attach the token to the response
   | so that it can be easily accessed in your application
   |
   */
   'return_user_token' => true,

4. Update your authentication logic:
   In your authentication controller (e.g., `LoginController`), make sure you are using the `JWTAuth` facade and include the `attempt` method. Here's an example:

   
   use Illuminate\Support\Facades\Auth;
   use Tymon\JWTAuth\Facades\JWTAuth;

   public function login(Request $request)
   {
       $credentials = $request->only('email', 'password');

       if ($token = JWTAuth::attempt($credentials)) {
           return response()->json([
               'token' => $token,
               'user' => Auth::user(), // Include user details if needed
           ]);
       }

       return response()->json(['error' => 'Unauthorized'], 401);
   }
  

By setting `'return_user_token'` to `true` in the configuration file and modifying your authentication logic, you should receive the token in the response upon successful authentication. Adjust the code as needed based on your application structure and requirements. 

Wednesday, January 3, 2024

How to set default value to spinner dropdown in Android?

 In Android, if you want to set a default value for a `Spinner` dropdown, you can achieve this by adding the default value to the adapter and selecting it programmatically. Here's an example:


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = findViewById(R.id.spinner);
        // Create a list of items for the spinner
        List<String> items = new ArrayList<>();
        items.add("Default Value"); // Add your default value here
        items.add("Item 1");
        items.add("Item 2");
        items.add("Item 3");
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        // Set the default value as the selected item
        spinner.setSelection(0); // 0 is the index of the default value
        // Set a listener to handle spinner item selection events
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // Handle the selected item
                String selectedValue = (String) parentView.getItemAtPosition(position);
                // Do something with the selected value
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // Do nothing here
            }
        });
    }
}


In this example, "Default Value" is added to the list of items, and `spinner.setSelection(0);` is used to set it as the default selected item. Adjust the code based on your specific use case and UI elements.

Tuesday, January 2, 2024

Laravel artisan migrate PHP Fatal error: Cannot declare class, because the name is already in use : Solution

When I was trying to migrate a database in Laravel Project using the 'php artisan migrate', I get the below error:

php artisan migrate

Migration table created successfully.

PHP Fatal error:  Cannot declare class CreatePasswordResetsTable, because the name is already in use in F:\Projects\database\migrations\2023_11_28_102415_create_password_resets_table.php on line 7    7

[Symfony\Component\Debug\Exception\FatalErrorException]

Cannot declare class CreatePasswordResetsTable, because the name is already in use


In such error, we can check below solutions:

First Solution :

It seems like you have 2 migrations done at different time with essentially same name.
for example : 2019_01_18_020910_create_Password_Reset_table.php
and 2019_01_16_020910_create_Password_Reset_table.php
Laravel will convert this filename eliminating the date signature and Camel Casing the remaining text.
So both of these migration will have class CreatePasswordResetsTableeven if the time signatures are different. Check if your migrations directory have such 2 files.
To check this run this from terminal in project root : 
grep -ri 'CreatePasswordResetsTable' database/migrations
You can delete one of the old migration file with the same table name. And run 'php artisan migrate'. It should work.
Second Solution :
Sometimes composer's internal class autoloading causes this issue. Do following to check if it resolves :
run composer install
Third Solution :
This is likely to be invalid but a same file should not have same class declaration 2 files by mistake.
Fourth Solution :
There might be a package you have installed which has a migration with same class name. To find run grep -ril'CreatePasswordResetsTable ' Vendor

If it shows any file then that's what causing 2 classes to have same names.

You can create a new one php artisan make:migration create_password_reset_table_custom and then copy what you have in current migration to the new one and delete the existing one(not from package but the one you have created).

This will create a class CreatePasswordResetsTable which is different than what the package already has.


One of the above solution will solve the issue