How to create custom ID Generator in Laravel 8

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024

Комментарии • 65

  • @udaramadarasinghe
    @udaramadarasinghe 10 месяцев назад +1

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

  • @jamalbayacine3829
    @jamalbayacine3829 3 года назад +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.

  • @Alerodb2020
    @Alerodb2020 2 года назад +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.

    • @sawastacks
      @sawastacks  2 года назад

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

    • @blank_7593
      @blank_7593 2 года назад

      thank you for your help :)

  • @voskilltheentertainer2564
    @voskilltheentertainer2564 2 года назад

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

  • @nelli.khimura
    @nelli.khimura Год назад

    very helpful. Thank yoyu

  • @ທາງລັດ
    @ທາງລັດ 2 года назад

    This is great tutorial video, thanks for sharing.

  • @ToussaintMufungizi
    @ToussaintMufungizi 3 года назад +1

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

  • @eliebea1307
    @eliebea1307 2 года назад +1

    Thanks 🔥

    • @sawastacks
      @sawastacks  2 года назад

      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.

  • @blank_7593
    @blank_7593 2 года назад

    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 3 года назад

    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?

  • @naufalnadaandesa7799
    @naufalnadaandesa7799 2 года назад

    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

  • @SagradaBiblia2050
    @SagradaBiblia2050 4 года назад +1

    Thank you so much for your help

  • @seemabaloda6250
    @seemabaloda6250 3 года назад

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

  • @kevinwakhisi7001
    @kevinwakhisi7001 3 года назад

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

  • @kevinwakhisi7001
    @kevinwakhisi7001 3 года назад +1

    damn another great video you just earn my sub

  • @ajith1786
    @ajith1786 3 года назад

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

  • @alphonsesossou4283
    @alphonsesossou4283 3 года назад

    Thanks, excellent and really useful tutorial.

  • @sonihidayatulloh9242
    @sonihidayatulloh9242 Год назад +1

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

    • @sawastacks
      @sawastacks  Год назад

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

  • @Defaulttt
    @Defaulttt 3 года назад +1

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

    • @sawastacks
      @sawastacks  3 года назад +1

      Thanks for the sub!

    • @ronanjoziah6966
      @ronanjoziah6966 3 года назад

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

    • @caseymyles7202
      @caseymyles7202 3 года назад

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

  • @code4u620
    @code4u620 3 года назад

    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

    • @sawastacks
      @sawastacks  3 года назад

      Just change this function like this

  • @gamehay24g97
    @gamehay24g97 Год назад

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

  • @ThuyLe-gu7cy
    @ThuyLe-gu7cy 3 года назад

    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?

    • @sawastacks
      @sawastacks  3 года назад

      Have you check $actia_last_number => $actial_last_number ?

    • @ThuyLe-gu7cy
      @ThuyLe-gu7cy 3 года назад

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

    • @sawastacks
      @sawastacks  3 года назад

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

  • @alanali7177
    @alanali7177 3 года назад

    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.

    • @sawastacks
      @sawastacks  3 года назад +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 3 года назад

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

    • @sawastacks
      @sawastacks  3 года назад

      @Aya Media Oooh! Am going to check that

    • @alanali7177
      @alanali7177 3 года назад

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

  • @praveenkusugal3279
    @praveenkusugal3279 3 года назад

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

    • @alphonsesossou4283
      @alphonsesossou4283 3 года назад

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

  • @stephenawuor8817
    @stephenawuor8817 3 года назад

    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"

    • @sawastacks
      @sawastacks  3 года назад

      Let me check

    • @stephenawuor8817
      @stephenawuor8817 3 года назад

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

    • @sawastacks
      @sawastacks  3 года назад

      @@stephenawuor8817 Yeah. show me

    • @stephenawuor8817
      @stephenawuor8817 3 года назад

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

    • @sawastacks
      @sawastacks  3 года назад

      @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

  • @abeselomlandber9290
    @abeselomlandber9290 3 года назад

    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.

    • @sawastacks
      @sawastacks  3 года назад

      $actual_last_number = ($code/1)*1;

    • @saimazainab6995
      @saimazainab6995 3 года назад

      I'm also getting the same error.

    • @targetx1733
      @targetx1733 2 года назад

      You have to do cast $core=(int) $core

  • @nathasyapradita4012
    @nathasyapradita4012 3 года назад

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

    • @alphonsesossou4283
      @alphonsesossou4283 3 года назад

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

    • @hidjrahs
      @hidjrahs 3 года назад

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

    • @sonihidayatulloh9242
      @sonihidayatulloh9242 Год назад

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

  • @dewawahyu9115
    @dewawahyu9115 3 года назад

    How to add year bro ? STR-00000-2021

    • @cristianbilu
      @cristianbilu 3 года назад

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

    • @alphonsesossou4283
      @alphonsesossou4283 3 года назад

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

  • @aryayudhareynardo
    @aryayudhareynardo 2 года назад

    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 2 года назад

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