laravel Eloquent unique code example
Example 1: get unique values in laravel
$users = User::select('name')->distinct()->get();
Example 2: laravel set field unique
Schema::table('manufacturers', function($table)
{
$table->string('name')->unique();
});
Example 3: laravel unique id
function generateBarcodeNumber() {
$number = mt_rand(1000000000, 9999999999);
if (barcodeNumberExists($number)) {
return generateBarcodeNumber();
}
return $number;
}
function barcodeNumberExists($number) {
return User::whereBarcodeNumber($number)->exists();
}