Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Laravel Errors

Error1

local.ERROR: Using $this when not in object context {"userId":21,"exception":"[object] (Error(code: 0): Using $this when not in object context at C:\xampp\htdocs\wz-account-admin-ms\app\Http\Controllers\Admin\PaymentController.php:1964

my code is

 public static function approvetask($id)

    {    
        $approvetask  = $this->task->get_task($id);

   }
Enter fullscreen mode Exit fullscreen mode

Solution
Using $this when not in object context," indicates that you are trying to use the $this keyword in a static method. In PHP, $this is used to refer to the current instance of a class when you're inside an object method (i.e., not in a static context). Static methods do not have access to the $this variable because they operate on the class itself, not on an instance of the class

If you want to use a method or property of a class within a static method, you'll need to create an instance of the class first. Here's how you can modify your code to avoid the error

 public static function approvetask($id)

    {    
        $task = new PaymentController(); 
        $approvetask  = $task->get_task($id);

        log::info("approvetask aata hain");
        log::info($id);
        log::info($approvetask);
        return response()->json([

            'approvetask' => $approvetask,
        ]);
        }
Enter fullscreen mode Exit fullscreen mode

Error2:Illegal mix of collations while left join with db::table in laravel

my code is

$data = DB::connection('payments')
->table('addprofiles')

->leftJoin('countries', 'addprofiles.country_id', '=', 'countries.country_id')
->leftJoin('addcarts', 'addprofiles.user_email', '=', 'addcarts.admin_email')
->leftJoin('states', 'addprofiles.state_id', '=', 'states.state_id')
->leftJoin('cities', 'addprofiles.city_id', '=', 'cities.city_id')
->leftJoin('users', 'addprofiles.user_id', '=', 'users.id')
->leftJoin('payments', 'payments.admin_id', '=', 'addprofiles.user_id')
->select('addprofiles.*', 'countries.country_name', 'states.state_name','addcarts.influencer_email', 'cities.city_name', 'addprofiles.file_pic', 'payments.admin_id');

Solution

->leftJoin('addcarts', DB::raw('BINARY addprofiles.user_email'), '=', DB::raw('BINARY addcarts.admin_email'))
Enter fullscreen mode Exit fullscreen mode
  $data = DB::connection('payments')
                ->table('addprofiles')              
                ->leftJoin('countries', 'addprofiles.country_id', '=', 'countries.country_id')
                ->leftJoin('addcarts', DB::raw('BINARY addprofiles.user_email'), '=', DB::raw('BINARY addcarts.admin_email'))
                ->leftJoin('states', 'addprofiles.state_id', '=', 'states.state_id')
                ->leftJoin('cities', 'addprofiles.city_id', '=', 'cities.city_id')
                ->leftJoin('users', 'addprofiles.user_id', '=', 'users.id')
                ->leftJoin('payments', 'payments.admin_id', '=', 'addprofiles.user_id')
                ->select('addprofiles.*', 'countries.country_name', 'states.state_name','addcarts.influencer_email', 'cities.city_name', 'addprofiles.file_pic', 'payments.admin_id');
Enter fullscreen mode Exit fullscreen mode

Reason

When dealing with the "Illegal mix of collations" error while using Laravel's DB::table and performing LEFT JOIN operations, you should follow a checklist to resolve the issue. Below is a list of steps and considerations, along with examples, to help you solve the collation mismatch problem:

Check Collations:

Ensure that the collations of columns used in LEFT JOIN conditions match. Specifically, check the collations of columns involved in joining and conditions.
Alter Column Collation:

If collations don't match, you may need to change the collation of the column(s) to match. Use the ALTER TABLE statement to modify the column's collation.
Example:

ALTER TABLE table_name MODIFY column_name column_type COLLATE utf8mb4_unicode_ci;
Enter fullscreen mode Exit fullscreen mode

Verify Database Collation:

Check the collation of the database itself. Ensure it matches with the collation specified in your Laravel configuration.
Laravel Configuration:

Ensure that your Laravel database configuration (config/database.php) specifies the correct character set and collation.

'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
Enter fullscreen mode Exit fullscreen mode

Use Raw Expressions:

In cases where you can't change the collation or when using DB::table, you may use raw expressions to force matching collations for comparisons.
Example:

->leftJoin('addcarts', DB::raw('BINARY addprofiles.user_email'), '=', DB::raw('BINARY addcarts.admin_email'))
Enter fullscreen mode Exit fullscreen mode

Debugging:

Use Laravel's toSql method to see the SQL query generated by your query builder. This can help you identify where collation-related issues might be.
Example:

$query = DB::table('addprofiles')
           ->leftJoin('addcarts', 'addprofiles.user_email', '=', 'addcarts.admin_email')
           ->toSql();
Enter fullscreen mode Exit fullscreen mode

Database Administrator:

If none of the above solutions work, consult your database administrator or hosting provider for assistance in resolving collation issues at the database level.
Ensure Consistency:

Maintain consistency in collations across all tables and columns, especially when designing your database schema.

Error3:Attempt to read property "order_id" on array

my code is 
log::info($users);

        return view('pages.orderconfirm', compact('users', 'currentURL'));
Enter fullscreen mode Exit fullscreen mode

the output of log::info($users);

[2023-11-08 06:37:04] local.INFO: array (
  'id' => 263,
  'payment_id' => 'PAYID-ORDS8852925',
  'influencer_admin_id' => '[20]',
  'payer_id' => NULL,
  'payer_email' => NULL,
  'amount' => 1.1,
  'currency' => NULL,
  'payment_status' => 'approved',
  'admin_id' => '28',
  'user_name' => 'rakesh',
  'Pay_date' => '2023-11-08',
  'cart_id' => 'CART-blvg-28',
  'product_id' => '[208]',
  'slug' => NULL,
  'org_slug' => NULL,
  'order_id' => 'ORD-1699421648-8413',
  'admin_email' => 'rakeshdev.cotocus@gmail.com',
  'influencer_email' => '["xyz@gmail.com"]',
  'influencer_name' => '["Roshan"]',
  'created_at' => '2023-11-08T06:32:39.000000Z',
  'updated_at' => '2023-11-08T05:34:08.000000Z',
  'orders_id' => 'ORD-1699421648-8413',
) 
Enter fullscreen mode Exit fullscreen mode

in blade file

<div class="card-body">

                    <div class="row">

                    <div class="col-md-6">

                        <p>Order Id:   {{$users->order_id}}</p>

                    </div>

                    <div class="col-md-6">

                        <p>Payment-Id: {{$users->payment_id}}</p>

                    </div>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)