mardi 7 février 2023

500 Internal Server Error in Laravel query error when i die dump the data appears but the console show ther error

 $shipments =DB::table('users')
    ->select('users.fname')
    //  ->where('invnum.doctype','=',5)
    //  ->where('invnum.docstate','=',4)
   
    
     ->get();

     dd($shipments);

i have an issue with my laravel application which just started abruptly am getting the below error enter image description here

The data is displays in dd but when i have a query with a lot of data it doesn't and am getting the error i have tried to add cors but the error doent go away what could be the issue the laravel logs shows no error. I understand 500 is not an actual error but the server could not find appropriate response



via Chebli Mohamed

Datadog: Manual Instrumentation in Laravel 5

I'm trying to manually add datadog tracing for my method in laravel. This involves creating a span on top of existing spans. I read this issue and tried both solutions but neither worked.

In the first approach, I ended up getting errors about dd_trace not being found (in my provider) even though dd_trace was up and running. I tried using \DDTrace\trace_method but I got errors about DDTrace not being found even though i could use DDTrace in my controller

In the second approach, I did not get any errors but nothing showed up in my datadog UI.

Not sure why this is happening. Any help would be much appreciated! Thanks



via Chebli Mohamed

lundi 6 février 2023

forceDelete method not working as expected in laravel 5 project (can't delete model from the database)

I working (editing) on project base on laravel 5.8 in the database I have "contacts" table main columns are :

( id ,name, .. ,deleted_at , deleted_by agency_id contactable_id ... )

so when is use $model->delete() for soft delete it will work very well

before delete ( id :1 ,name: 'name', .. ,deleted_at : null , deleted_by :null agency_id : 1 contactable_id : 1)

after delete ( id :1 ,name: 'name', .. ,deleted_at : (dateValue) , deleted_by :1 agency_id : 1 contactable_id : 1)

class ContactController extends Controller
{
 public function delete(Request $request)
    {


        if ($request->ajax()) {
            \Log::info($request->contact_id);
            try {
                $deleted = $this->model_instance::find($request->contact_id)->delete();
            } catch (\Exception $ex) {
                \Log::info($ex->getMessage());
            }
            if ($deleted) {
                $log_message = trans('contacts.delete_log') . '#' . $request->contact_id;
                //logActivity($log_message);
                return response()->json(['status' => 'success', 'message' => 'deleted_successfully']);
            } else {
                return response()->json(['status' => 'fail', 'message' => 'fail_while_delete']);
            }
        }

        return redirect()->route($this->index_route);
    }
}

so when is use $model->forceDelete() for delete for ever from the database (forceDelete) it will not work as expected but will fill deleted_by with user number and keep deleted_at with null value

before force delete ( id :1 ,name: 'name', .. ,deleted_at : null , deleted_by :null agency_id : 1 contactable_id : 1)

after force delete ( id :1 ,name: 'name', .. ,deleted_at : null , deleted_by :1 agency_id : 1 contactable_id : 1)

class ContactController extends Controller
{
    public function forceDelete(Request $request)
    {
        if ($request->ajax()) {
            \Log::info($request->contact_id);
            try {
                $deleted = $this->model_instance::withTrashed()->find($request->contact_id)->forceDelete();
              
            } catch (\Exception $ex) {
                \Log::info($ex->getMessage());
            }
            // if ($deleted) {
            //     $log_message = trans('contacts.delete_log') . '#' . $request->contact_id;
            //     return response()->json(['status' => 'success', 'message' => 'deleted_successfully']);
            // } else {
            //     return response()->json(['status' => 'fail', 'message' => 'fail_while_delete']);
            // }
        }
        // return redirect()->route($this->index_route);
    }
}

forceDelete() return true


so this method work like this or there problem in my code in some where(I mean not jsut the previos code ) ?!

use softdelete in some cases use delete (for soft delete) and other cases use forceDelete (for delete from the database)


for soft delete it worked very well for force delete it takes another scenario



via Chebli Mohamed

Carbon's formatLocalized not working on production server

The following function present in app/Helpers/helpers.php returns me the short month on my local machine. However, I deployed it to the server and it is always returning the month in English.

function dateFormatShortDateLang($date) {
    setlocale(LC_TIME, app()->getLocale());
    \Carbon\Carbon::setLocale(app()->getLocale());
    $shortMonth = ucfirst(str_replace('.', ',', \Carbon\Carbon::createFromFormat('d/m/Y', $date)->formatLocalized('%b %G')));

    return $shortMonth;
}

If I dd() the value of app()->getLocale() I get the correct current locale. The whole repo has the same code, same PHP version and I run the following commands:

php artisan cache:clear
php artisan config:clear
composer dump autoload

My config/app.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Application Name
    |--------------------------------------------------------------------------
    |
    | This value is the name of your application. This value is used when the
    | framework needs to place the application's name in a notification or
    | any other location as required by the application or its packages.
    |
    */

    'name' => env('APP_NAME', 'Laravel'),

    /*
    |--------------------------------------------------------------------------
    | Application Environment
    |--------------------------------------------------------------------------
    |
    | This value determines the "environment" your application is currently
    | running in. This may determine how you prefer to configure various
    | services your application utilizes. Set this in your ".env" file.
    |
    */

    'env' => env('APP_ENV', 'production'),

    /*
    |--------------------------------------------------------------------------
    | Application Debug Mode
    |--------------------------------------------------------------------------
    |
    | When your application is in debug mode, detailed error messages with
    | stack traces will be shown on every error that occurs within your
    | application. If disabled, a simple generic error page is shown.
    |
    */

    'debug' => env('APP_DEBUG', false),

    /*
    |--------------------------------------------------------------------------
    | Application URL
    |--------------------------------------------------------------------------
    |
    | This URL is used by the console to properly generate URLs when using
    | the Artisan command line tool. You should set this to the root of
    | your application so that it is used when running Artisan tasks.
    |
    */

    'url' => env('APP_URL', 'http://localhost'),

    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'UTC',

    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'es',

    /*
    |--------------------------------------------------------------------------
    | Application Fallback Locale
    |--------------------------------------------------------------------------
    |
    | The fallback locale determines the locale to use when the current one
    | is not available. You may change the value to correspond to any of
    | the language folders that are provided through your application.
    |
    */

    'fallback_locale' => 'es',

    /*
    |--------------------------------------------------------------------------
    | Encryption Key
    |--------------------------------------------------------------------------
    |
    | This key is used by the Illuminate encrypter service and should be set
    | to a random, 32 character string, otherwise these encrypted strings
    | will not be safe. Please do this before deploying an application!
    |
    */

    'key' => env('APP_KEY'),

    'cipher' => 'AES-256-CBC',

    /*
    |--------------------------------------------------------------------------
    | Logging Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log settings for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Settings: "single", "daily", "syslog", "errorlog"
    |
    */

    'log' => env('APP_LOG', 'single'),

    'log_level' => env('APP_LOG_LEVEL', 'debug'),

    /*
    |--------------------------------------------------------------------------
    | Autoloaded Service Providers
    |--------------------------------------------------------------------------
    |
    | The service providers listed here will be automatically loaded on the
    | request to your application. Feel free to add your own services to
    | this array to grant expanded functionality to your applications.
    |
    */

    'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        // Illuminate\Translation\TranslationServiceProvider::class,
        Spatie\TranslationLoader\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,

        // Multilingual
        App\Providers\RouteServiceProvider::class,
        App\Providers\ViewComposerServiceProvider::class,

    ],

    /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        'Blade' => Illuminate\Support\Facades\Blade::class,
        'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
        'Bus' => Illuminate\Support\Facades\Bus::class,
        'Cache' => Illuminate\Support\Facades\Cache::class,
        'Config' => Illuminate\Support\Facades\Config::class,
        'Cookie' => Illuminate\Support\Facades\Cookie::class,
        'Crypt' => Illuminate\Support\Facades\Crypt::class,
        'DB' => Illuminate\Support\Facades\DB::class,
        'Eloquent' => Illuminate\Database\Eloquent\Model::class,
        'Event' => Illuminate\Support\Facades\Event::class,
        'File' => Illuminate\Support\Facades\File::class,
        'Gate' => Illuminate\Support\Facades\Gate::class,
        'Hash' => Illuminate\Support\Facades\Hash::class,
        'Lang' => Illuminate\Support\Facades\Lang::class,
        'Log' => Illuminate\Support\Facades\Log::class,
        'Mail' => Illuminate\Support\Facades\Mail::class,
        'Notification' => Illuminate\Support\Facades\Notification::class,
        'Password' => Illuminate\Support\Facades\Password::class,
        'Queue' => Illuminate\Support\Facades\Queue::class,
        'Redirect' => Illuminate\Support\Facades\Redirect::class,
        'Redis' => Illuminate\Support\Facades\Redis::class,
        'Request' => Illuminate\Support\Facades\Request::class,
        'Response' => Illuminate\Support\Facades\Response::class,
        'Route' => Illuminate\Support\Facades\Route::class,
        'Schema' => Illuminate\Support\Facades\Schema::class,
        'Session' => Illuminate\Support\Facades\Session::class,
        'Storage' => Illuminate\Support\Facades\Storage::class,
        'URL' => Illuminate\Support\Facades\URL::class,
        'Validator' => Illuminate\Support\Facades\Validator::class,
        'View' => Illuminate\Support\Facades\View::class,

    ],
];


via Chebli Mohamed

vendredi 3 février 2023

Laravel copy and move not working with large file(10GB) with s3

Small files (up to 1 GB) are successfully transferred but large files return false on the move and copy functions.

both functions should work correctly for large files as well.

My code

try {
       //dd($param);
       $s3 = Storage::disk('s3');
       $dd = $s3->move($param['source'], $param['target']);
       dd($dd);
    } catch (Exception $e) {
       return [
          "status" => 0,
             "msg" => $e->getMessage(),
         ];
   }


via Chebli Mohamed

mercredi 1 février 2023

laravel dynamic routing showing 404 not found

I have created a route to pass dynamic parameters to controller but when i serve url it shows me for not found here is my route and controller I create

Routes

Route::get('/admin/managers/{method}/{id}', 
    [
        SetupController::class, 'managers'
    ]
);

Controller

public function managers($method, $id) {
        if($method == 'create') {
            return view('admin.pages.create-manager'); 
        } elseif($method == 'create') {
            echo $id;
            return view('admin.pages.create-manager'); 
        } else {
            return view('admin.pages.managers'); 
        }  
    }

When i serve url localhost/public/managers it shows me 404 not found but when i serve localhost/public/managers/create/1 the page is loading then can anyone hep me out why exactly is it happening



via Chebli Mohamed

Random project chooser for laravel

today I have two similar Laravel projects, they are exactly the same except for the Laravel version, the first is running on 5.5, and the second is the updated version running on Laravel 9. The Laravel 9 one is not in production because we need a way to test it and check if everything is working after the update. My question is if there's a way to redirect to some clients only the newer version. The projects for the clients are exactly the same.

In conclusion I need a way to show to like 20% of the clients the newer version, and the older version for the other 80%, is possible to do this? Is there a better way to test the newer project for errors?

Thanks in advance.



via Chebli Mohamed