How to create custom ID Generator in Laravel 8

Sdílet
Vložit
  • čas přidán 9. 11. 2020
  • 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/mberecall/Custom-I...
    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...
  • Jak na to + styl

Komentáře • 65

  • @udaramadarasinghe
    @udaramadarasinghe Před 7 měsíci +1

    Thank you so much. This video helped me to resolve one of my ID Generating issues.

  • @jamalbayacine3829
    @jamalbayacine3829 Před 3 lety +1

    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.

  • @Defaulttt
    @Defaulttt Před 3 lety +1

    You just earned a sub. I hope you'll do more in the future! Thank you!

    • @irebelibrary
      @irebelibrary  Před 3 lety +1

      Thanks for the sub!

    • @ronanjoziah6966
      @ronanjoziah6966 Před 3 lety

      i realize I am pretty off topic but does anyone know of a good place to watch new movies online ?

    • @caseymyles7202
      @caseymyles7202 Před 3 lety

      @Ronan Joziah lately I have been using FlixZone. You can find it by googling =)

  • @alphonsesossou4283
    @alphonsesossou4283 Před 3 lety

    Thanks, excellent and really useful tutorial.

  • @SagradaBiblia2050
    @SagradaBiblia2050 Před 3 lety +1

    Thank you so much for your help

  • @nelli.khimura
    @nelli.khimura Před rokem

    very helpful. Thank yoyu

  • @user-xr8yt1bx9v
    @user-xr8yt1bx9v Před 2 lety

    This is great tutorial video, thanks for sharing.

  • @kevinwakhisi7001
    @kevinwakhisi7001 Před 3 lety +1

    damn another great video you just earn my sub

  • @blank_7593
    @blank_7593 Před 2 lety

    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?

  • @azmmifauzy2195
    @azmmifauzy2195 Před 2 lety

    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?

  • @eliebea1307
    @eliebea1307 Před rokem +1

    Thanks 🔥

    • @irebelibrary
      @irebelibrary  Před rokem

      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.

  • @seemabaloda6250
    @seemabaloda6250 Před 3 lety

    if i use this code using facade then facade is define in place of $model.plz tell

  • @ToussaintMufungizi
    @ToussaintMufungizi Před 2 lety +1

    Since it is capturing max ID What happens if two users are trying to insert new student simultaneously????

  • @naufalnadaandesa7799
    @naufalnadaandesa7799 Před 2 lety

    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

  • @voskilltheentertainer2564

    this tutorial saved me.how can i pass the custom id generated to an api endpoint?

  • @Alerodb2020
    @Alerodb2020 Před 2 lety +3

    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.

    • @irebelibrary
      @irebelibrary  Před 2 lety

      Hello alexander.
      I hadn't tried it in laravel 9.
      Thank you so much for the correction

    • @blank_7593
      @blank_7593 Před 2 lety

      thank you for your help :)

  • @kevinwakhisi7001
    @kevinwakhisi7001 Před 3 lety

    i have a question, how can i reset the counter yearly or monthly, lets say STD-2112001 then next year STD-2201001

  • @ajith1786
    @ajith1786 Před 3 lety

    can you please add this code to Github repository. Nice video Thanks

  • @code4u620
    @code4u620 Před 3 lety

    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

  • @ThuyLe-gu7cy
    @ThuyLe-gu7cy Před 3 lety

    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?

    • @irebelibrary
      @irebelibrary  Před 3 lety

      Have you check $actia_last_number => $actial_last_number ?

    • @ThuyLe-gu7cy
      @ThuyLe-gu7cy Před 3 lety

      @@irebelibrary I solve the problem, because about database on mysql, i change to int -> varchar then is work

    • @irebelibrary
      @irebelibrary  Před 3 lety

      @@ThuyLe-gu7cy nice! I already add link of this on description.

  • @praveenkusugal3279
    @praveenkusugal3279 Před 3 lety

    1364 Field 'customer_id' doesn't have a default value (SQL: insert into `contacts`

    • @alphonsesossou4283
      @alphonsesossou4283 Před 3 lety

      $table->string(''customer_id')->nullable(); in migration file

  • @abeselomlandber9290
    @abeselomlandber9290 Před 3 lety

    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.

  • @sonihidayatulloh9242
    @sonihidayatulloh9242 Před rokem +1

    how first number id i want first number one not a zero how i can try in code, please help me

    • @irebelibrary
      @irebelibrary  Před rokem

      Can you try our laravel packages: github.com/mbere250/laravel-idify

  • @alanali7177
    @alanali7177 Před 3 lety

    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.

    • @irebelibrary
      @irebelibrary  Před 3 lety +1

      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 */

    • @alanali7177
      @alanali7177 Před 3 lety

      @@irebelibrary 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.

    • @irebelibrary
      @irebelibrary  Před 3 lety

      @Aya Media Oooh! Am going to check that

    • @alanali7177
      @alanali7177 Před 3 lety

      @Aya Media it's working, Thank you so much sir.

  • @stephenawuor8817
    @stephenawuor8817 Před 2 lety

    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"

    • @irebelibrary
      @irebelibrary  Před 2 lety

      Let me check

    • @stephenawuor8817
      @stephenawuor8817 Před 2 lety

      @@irebelibrary Thank you, would you want to see my code?

    • @irebelibrary
      @irebelibrary  Před 2 lety

      @@stephenawuor8817 Yeah. show me

    • @stephenawuor8817
      @stephenawuor8817 Před 2 lety

      @@irebelibrary
      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!');
      }

    • @irebelibrary
      @irebelibrary  Před 2 lety

      @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:
      czcams.com/video/Mr5hRns6U50/video.html

  • @nathasyapradita4012
    @nathasyapradita4012 Před 3 lety

    Hi, how to change the start number from "0" To "1" So the first number is 00001 not 00000?

    • @alphonsesossou4283
      @alphonsesossou4283 Před 3 lety

      Use this code:
      if(!$data){
      $og_length = $length;
      $last_number = 1;
      }else{..........

    • @hidjrahs
      @hidjrahs Před 2 lety

      @@alphonsesossou4283 if(!$data){
      $og_length = $length-1;
      $last_number = 1;
      }else{..........

    • @sonihidayatulloh9242
      @sonihidayatulloh9242 Před rokem

      @@hidjrahs i error in $actial_last_number = ($code/1)*1; in message error A non-numeric value encountered

  • @gamehay24g97
    @gamehay24g97 Před rokem

    Tôi muốn thêm nhiều trong 1 lần thì phải làm sao

  • @dewawahyu9115
    @dewawahyu9115 Před 3 lety

    How to add year bro ? STR-00000-2021

    • @cristianbilu
      @cristianbilu Před 3 lety

      Add date("Y") at the end of the return as a suffix

    • @alphonsesossou4283
      @alphonsesossou4283 Před 3 lety

      @@cristianbilu return $prefix.'-'.$zeros.$number.date('Y);

  • @aryayudhareynardo
    @aryayudhareynardo Před 2 lety

    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');
    }

    • @sativasetyadji1288
      @sativasetyadji1288 Před 2 lety

      Stok::Create([
      'kode_barang' => Helper::IDGenerator(new Stok, 'kode_barang', 5, 'KB-');
      'nama' => $request->nama,
      'kategori_id' => $request->kategori_id,
      ]);