How to create custom ID Generator in Laravel 8
HTML-код
- Опубликовано: 27 сен 2024
- In this video, we will show you how to make custom id generator in laravel 8. The ID generator takes a unique external ID in the form of a string and returns a unique ID, which can be used as an item ID (for example, TLS006, STD-0005, INV_5750). You can use this example in order to make your own custom id. You can use this when you want to make Invoices, appointments, students , etc IDs.
Get this project Github repository:
github.com/mbe...
If you didn't know how we created Helper, please watch the following video: • How to create custom h...
You can watch these videos:
Send email using phpmailer in laravel 8:
• How to Send email usin...
Upload file with auto rename if exists in laravel 8:
• Laravel 8 tutorial | U...
Create Custom ID generator in Laravel 8:
• How to create custom I...
Make custom helper in Laravel 8:
• How to create custom h...
Fix localhost and phpmyadmin not working when you use xampp:
• How to fix localhost a...
PHP not recognized as external or internal command:
• PHP is not recognized ...
Register and Login in PHP with jquery validation | part 1
• Login and Register in ...
Register and Login in PHP with jquery validation | part 2
• Login and Register in ...
Upload file in php with validation:
• How to upload file wit...
In laravel 9 with PHP 8.1.2 it shows this error “Unsupported operand types: string / int” when saving for the first time.
change the line
$code = substr($data->$trow, strlen($prefix)+1);
to
$code = (int)(substr($data->$trow, strlen($prefix)+1));
With that it works for me, thank you very much for the idea of the IDGenerator.
Hello alexander.
I hadn't tried it in laravel 9.
Thank you so much for the correction
thank you for your help :)
This is great tutorial video, thanks for sharing.
Glad it was helpful!
Thank you so much for your help
merci beaucoup!
Thanks, excellent and really useful tutorial.
Glad you enjoyed it!
very helpful. Thank yoyu
Thank you so much. This video helped me to resolve one of my ID Generating issues.
damn another great video you just earn my sub
how to create 2 different idgenerator (different prefix) on the same table. Like STD-0001, STD-0002 then in another prefix ABC-0001, ABC-0002 and so on. can you help me?
Bro i was succeed generating Id like INV-00001 .. but when i try to get on my view... it just show 0... so how to get the original value of my id?
how first number id i want first number one not a zero how i can try in code, please help me
Can you try our laravel packages: github.com/mbere250/laravel-idify
i have a question, how can i reset the counter yearly or monthly, lets say STD-2112001 then next year STD-2201001
if i use this code using facade then facade is define in place of $model.plz tell
can you please add this code to Github repository. Nice video Thanks
Hi Got This ErrorException A non-numeric value encountered, when i add 3 or 4 more student at the code $actia_last_number = ($code/1)*1; do you know how to solve this problem?
Have you check $actia_last_number => $actial_last_number ?
@@sawastacks I solve the problem, because about database on mysql, i change to int -> varchar then is work
@@ThuyLe-gu7cy nice! I already add link of this on description.
How to add year bro ? STR-00000-2021
Add date("Y") at the end of the return as a suffix
@@cristianbilu return $prefix.'-'.$zeros.$number.date('Y);
hello, i have problem can you help me? Undefined variable: data
my code : controller
public function store(Request $request)
{
$kode_barang = Helper::IDGenerator(new Stok, 'kode_barang', 5, 'KB-');
Stok::Create([
'kode_barang' => $kode_barang,
'nama' => $request->nama,
'kategori_id' => $request->kategori_id,
]);
return redirect('/stok-barang');
}
Stok::Create([
'kode_barang' => Helper::IDGenerator(new Stok, 'kode_barang', 5, 'KB-');
'nama' => $request->nama,
'kategori_id' => $request->kategori_id,
]);
Since it is capturing max ID What happens if two users are trying to insert new student simultaneously????
Tôi muốn thêm nhiều trong 1 lần thì phải làm sao
I think you have to note these instructions :
if($last_number_length>$length){$og_length=0;}
else{$og_length=$length-$last_number_length;}
thank you for this video.
Thanks 🔥
Hi Elie Bea. you can use my package: github.com/mbere250/laravel-idify for creating custom ID. That package will do the same to this.
I have an error its "Attempt to read property "no" on null". "no" is like the number id of document and the error was in this line : $code=(substr($data->$trow, strlen($prefix)+1));
how to solve this sir, thanks before
this tutorial saved me.how can i pass the custom id generated to an api endpoint?
1364 Field 'customer_id' doesn't have a default value (SQL: insert into `contacts`
$table->string(''customer_id')->nullable(); in migration file
You just earned a sub. I hope you'll do more in the future! Thank you!
Thanks for the sub!
i realize I am pretty off topic but does anyone know of a good place to watch new movies online ?
@Ronan Joziah lately I have been using FlixZone. You can find it by googling =)
hi irebe i need help for the same situation where i have a employee-id i need to be save as emp001 but i a m failing to do it
Just change this function like this
Thank you so much! i'm getting "Unsupported operand types: string / int" error on helper.php where the line $actial_last_number = ($code/1)*1; is found.
$actual_last_number = ($code/1)*1;
I'm also getting the same error.
You have to do cast $core=(int) $core
Hi Irebe, thank you so much for your video, can you please tell me and how to fix this error,
ErrorException
Required parameter $prefix follows optional parameter $length
I use same your code in helper class.
make sure that this helper function has all parameters
public static function IDGenerator($model, $trow, $length = 4, $prefix){}
also in controller:
$student_id = Helper::IDGenerator(new Student, 'student_id', 2, 'STD'); /** Generate id */
@@sawastacks Thank you so much for your support, much appreciated.
I checked, but I have same error, This my code:
in contoller:
function insertDoctor(Request $request){
$request->validate([
'name'=>'required',
]);
$name = $request->name;
$email = $request->email;
$phone = $request->phone;
$address = $request->address;
$commission = $request->commission;
$doctor_id = mi::IDGenerator(new doctors, 'doctor_id', 2, 'Dr'); /** Generate id */
$q = new doctors;
$q->doctor_id = $doctor_id;
$q->name = $name;
$q->email = $email;
$q->phone = $phone;
$q->address = $address;
$q->commission = $commission;
$save = $q->save();
if($save){
return back()->with('success','New studen has been added');
}else{
return back()->with('faile','Something went wrong');
}
}
And I use custom helper class and I import:
use App\mi\mi;
inside ( mi ) class, I use same your code without edit it.
public static function IDGenerator($model, $trow, $length = 4, $prefix){}
And also make a route:
Route::post('insertdoctor',[AdminController::class,'insertDoctor'])->name('admin.insertDoctor'); and call that route in form:
action="{{ route('admin.insertDoctor') }}"
And I import also model class:
use App\Models\doctors;
this is code in model class
class doctors extends Model
{
use HasFactory;
protected $primaryKey = 'id';
protected $fillable = [
'doctor_id',
'name',
'email',
'phone',
'address',
'commission'
];
}
I think everything fine, but I don't know why give me that error.
@Aya Media Oooh! Am going to check that
@Aya Media it's working, Thank you so much sir.
Hi, how to change the start number from "0" To "1" So the first number is 00001 not 00000?
Use this code:
if(!$data){
$og_length = $length;
$last_number = 1;
}else{..........
@@alphonsesossou4283 if(!$data){
$og_length = $length-1;
$last_number = 1;
}else{..........
@@hidjrahs i error in $actial_last_number = ($code/1)*1; in message error A non-numeric value encountered
Hello, Thanks for the great video. Am getting an error though whenever I try to add a new student. Could you kindly assist?, My error is "Trying to get property 'STD-00001' of non-object"
Let me check
@@sawastacks Thank you, would you want to see my code?
@@stephenawuor8817 Yeah. show me
@@sawastacks
This is my code in the StudentsController, This function "add_student" is meant to add a new student. So I used your method to generate a random ID.
public function add_student(request $request){
$this->validate ($request, [
'a_date' => ['required'],
'a_year' => ['required'],
'a_grade' => ['required'],
'fname' => ['required', 'string', 'max:255'],
'mname' => ['string', 'max:255'],
'lname' => ['required', 'string', 'max:255'],
'gender' => ['required'],
'dob' => ['required'],
'c_school' => ['string', 'max:255'],
'parent' => ['string', 'max:255'],
'p_relation' => ['string'],
],[
'fname.required' => "The :value First Name is required.",
'lname.required' => "The :value Last Name is required.",
'a_grade.required' => "The :value Admission Grade is required.",
]);
$newstudent=new Students();
$Student_id = Helper::IDGenerator(new Students, 'Student_id', 4, 'STD');
$newstudent->a_date=$request->input('a_date');
$newstudent->a_year=$request->input('a_year');
$newstudent->a_grade=$request->input('a_grade');
$newstudent->fname=$request->input('fname');
$newstudent->mname=$request->input('mname');
$newstudent->lname=$request->input('lname');
$newstudent->gender=$request->input('gender');
$newstudent->dob=$request->input('dob');
$newstudent->c_school=$request->input('c_school');
$newstudent->parent=$request->input('parent');
$newstudent->p_relation=$request->input('p_relation');
$newstudent->Student_id->$Student_id;
$newstudent->save();
return redirect ('/admin_students')->with('success', 'New Student Successfully Added!');
}
@Stephen Awuor 1) student_id (s) must be lower case on this line:
$Student_id = Helper::IDGenerator(new Students, 'Student_id', 4, 'STD');
to $Student_id = Helper::IDGenerator(new Students, 'student_id', 4, 'STD');
2) make sure that you import this on this controller:
use App\Helpers\Helper;
use App\Models\Student;
use Validator;
3) Watch how you can create custom helper:
ruclips.net/video/Mr5hRns6U50/видео.html