Access denied for user ‘homestead’@’localhost’ (using password: YES)

The reason of Access denied for user ‘homestead’@’localhost’ laravel 5 error is caching-issue of the .env.php file cause Laravel 5 is using environment based configuration in your .env file. 1. Go to your application root directory and open .env file (In ubuntu may be it’s hidden so press ctrl+h to show hidden files & if … Read more

Laravel Check If Related Model Exists

In php 7.2+ you can’t use count on the relation object, so there’s no one-fits-all method for all relations. Use query method instead as @tremby provided below: $model->relation()->exists() generic solution working on all the relation types (pre php 7.2): if (count($model->relation)) { // exists } This will work for every relation since dynamic properties return … Read more

How to Get the Query Executed in Laravel 5? DB::getQueryLog() Returning Empty Array

By default, the query log is disabled in Laravel 5: https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448 You will need to enable the query log by calling: DB::enableQueryLog(); // and then you can get query log dd(DB::getQueryLog()); or register an event listener: DB::listen( function ($sql, $bindings, $time) { // $sql – select * from `ncv_users` where `ncv_users`.`id` = ? limit 1 … Read more

Laravel 5 – How to access image uploaded in storage within View?

The best approach is to create a symbolic link like @SlateEntropy very well pointed out in the answer below. To help with this, since version 5.3, Laravel includes a command which makes this incredibly easy to do: php artisan storage:link That creates a symlink from public/storage to storage/app/public for you and that’s all there is … Read more

How to query between two dates using Laravel and Eloquent?

The whereBetween method verifies that a column’s value is between two values. $from = date(‘2018-01-01’); $to = date(‘2018-05-02’); Reservation::whereBetween(‘reservation_from’, [$from, $to])->get(); In some cases you need to add date range dynamically. Based on @Anovative’s comment you can do this: Reservation::all()->filter(function($item) { if (Carbon::now()->between($item->from, $item->to)) { return $item; } }); If you would like to add … Read more

Laravel – create model, controller and migration in single artisan command

You can do it if you start from the model php artisan make:model Todo -mcr if you run php artisan make:model –help you can see all the available options -m, –migration Create a new migration file for the model. -c, –controller Create a new controller for the model. -r, –resource Indicates if the generated controller … Read more