Nothing to migrate in Laravel

If you are trying to migrate a single class you will do something like this

php artisan migrate --path = path_to_migration_file

if you get something like :

Nothing to migrate

and you are sure the file is there, then you can try to write the command like this

php artisan migrate --path="database/migrations/2022_11_04_1235_create_table.php"

Log Laravel

You can easily log your data in Larave, all you have to do is to use Log, and then log whatever you want, here is an example

namespace App\Http\Controllers;
use Log;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
     * Show the profile for the given user.
     * @param  int  $id
     * @return Response
    public function showProfile($id)
        Log::info('User ID: '.$id);
        return view('user.profile', ['user' => User::findOrFail($id)]);

You can also use an array of contextual data

Log::info('User failed to login.', ['id' => $user->id]);

Laravel provides 8 levels of logging


You can view the log file at: storage/laravel.log but you may find a huge file with thousands of line as this is the main file where all the logs are saved.

If you want to create your own log file, you will need to add a new channel in the config/logging.php

'channels' => [
        'myLog' => [
                    'driver' => 'single',
                    'path' => storage_path('logs/myLog.log'),
                    'level' => 'info',

where myLog.log is the file name you want to save your logs in

To write to your custom log file, you have to specify the channel you just created

Log::channel('myLog')->info('This is testing for my log file.');

ps. Don’t forget to empty the config cache for the updates to be active

from your terminal, you write: php artisan cache:clear

Finally, when you open myLog.log file, you will see something like this:

[2022-08-25 09:13:20] local.INFO: This is testing for my log file.

How to use withSum() with where condition

You can use withSum in query with where condition, here is an example:

If you want to list all Projects with the sum of all invoices that were issued this year:

$projects = Project::select('id', 'name', 'created_at')
                 ['invoices as prev_invoices' => function($query) {
                     $query->whereYear('invoices.issue_date', date('Y'));
                }], 'amount' 

How to set a function in a model as an attribute

If you have a model with a function that you want to use it as an attribute, all you have to do is to use protected $appends in the Model class

For example, you have a Project class with a function that gets the sum of all invoices for that project

class Project extends Model
protected $appends = ['prev_invoices'];
public function getPrevInvoicesAttribute()       
        $sum = Invoice::where('project_id', $this->id)
        return $sum;

Note that the function name must be formatted like that: “get” at the beginning, “Attribute” at the end, and the name to be CamelCase.

Laravel / PHP SOS

When your code is not working right and you have no idea what is wrong:

  • What is the language ?
    Yes, you heard me right, sometimes when you are writing javascript code inside a laravel/php page ..things could get mixed up
  • Remove the cache
    There are lots of cache types you need to clear depending on what you have changed, here are some commands to use:
    • php artisan config:cache
    • php artisan route:cache
    • php artisan view:cache
    • php artisan cache:clear
    • php artisan clear-compiled
    • composer dump-autoload
    • php artisan debugbar:clear
    • php artisan optimize
  • What is the type of the variables ?
    When you want to compare 2 variables and you are sure the result should be true, check the type of the variables, sometimes you could find that you are comparing a variable to an object or collection

How to update Magento 2

Try the CLI update process:

1) On your terminal go into magento root folder.
2) Set magento 2 under maintenance mode

php bin/magento maintenance:enable

3) Search for the correct and latest magento 2 version

composer show magento/product-community-edition 2.3.* --all | grep -m 1 versions

4) Force your composer to use the latest version

composer require magento/product-community-edition=2.3.3 --no-update

5) Update your magento 2 installation

composer update

6) After successful update Upgrade magento and compile

php bin/magento setup:upgrade
bin/magento setup:di:compile

7) Disable maintenance mode and you are ready to check the correct version

php bin/magento maintenance:disable


How to read file content in Public folder with Laravel

If you want to read the content of a file in the public folder using Laravel, you can use this code

$file_name = "test.dat";
$file_url = 'public\subfolder\\'. $file_name;
$content = file_get_contents(base_path($map_url));

base_path is a helper function to generate a fully qualified path to a given file relative to the project root directory

How to view latest SQL statement excuted in Laravel

To view the latest sql query executed in Laravel you can



Note that you have to enable the query log before executing the query and view/print it after execution ..

A good practice is to enable it only in Local environment

if (App::environment('local')) {
    // The environment is local


How to sanitize raw data in Laravel

When doing a raw data query from user input like this:

$someVariable = Input::get("some_variable");

$results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = '$someVariable'") );

we are at risk of SQL injection , to avoid that we can bin parameters to our query like this:

$someVariable = Input::get("some_variable");

$results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = :somevariable"), array(
   'somevariable' => $someVariable,


Another point is that if we want to do a raw that doesn’t return a value, we can do it like this

DB::statement( 'ALTER TABLE HS_Request AUTO_INCREMENT=1111' );

and that way can take parameters as well

DB::statement( 'ALTER TABLE HS_Request AUTO_INCREMENT=:incrementStart', array('incrementStart' => 1111) );