Laravel – Eloquent “Has”, “With”, “WhereHas” – What do they mean?

With with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of … Read more

Laravel – Eloquent or Fluent random row

Laravel >= 5.2: User::inRandomOrder()->get(); or to get the specific number of records // 5 indicates the number of records User::inRandomOrder()->limit(5)->get(); // get one random record User::inRandomOrder()->first(); or using the random method for collections: User::all()->random(); User::all()->random(10); // The amount of items you wish to receive Laravel 4.2.7 – 5.1: User::orderByRaw(“RAND()”)->get(); Laravel 4.0 – 4.2.6: User::orderBy(DB::raw(‘RAND()’))->get(); Laravel … Read more

No Application Encryption Key Has Been Specified

From Encryption – Laravel – The PHP Framework For Web Artisans: “Before using Laravel’s encrypter, you must set a key option in your config/app.php configuration file. You should use the php artisan key:generate command to generate this key” I found that using this complex internet query in google.com: “laravel add encrption key” (Yes, it worked … Read more

Laravel Checking If a Record Exists

It depends if you want to work with the user afterwards or only check if one exists. If you want to use the user object if it exists: $user = User::where(’email’, ‘=’, Input::get(’email’))->first(); if ($user === null) { // user doesn’t exist } And if you only want to check if (User::where(’email’, ‘=’, Input::get(’email’))->count() > … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)