Home > Software design >  I have a Number Counter in Laravel that only accepts integer values, but my values are stored in SQL
I have a Number Counter in Laravel that only accepts integer values, but my values are stored in SQL

Time:08-26

I have stored a value as a string . I am making a page dynamic, but in that template there is a number counter which only accepts an integer value .

Whenever I provide a value from database . code is here Line 5 data-to

<div >
                        <div >
                            <div >
                                <img src="{{asset('frontend/assets/demos/nonprofit/images/icons/breakfast.svg')}}" alt="Counter Icon" width="70" >
                                <div ><span data-from="100" data-to="{{ $counterdata->counter_1 }}" data-refresh-interval="50" data-speed="2100" data-comma="true"></span> </div>
                            </div>
                        </div>
                    </div>

data-to= The variable only accepts a number . Whenever I provide this, it gives my output like this.

enter image description here

So how can I convert that string to an integer and then provide that output to an html data-to variable? And if I convert this to an integer, will there be a comma because there is one in the database? So how can I pass the data so that it should also include commas?

  <form method="POST"  action="{{ route('update.counter') }}"   enctype="multipart/form-data" >
                                    @csrf

                                    <input type="hidden"  id="id" name="id" value="{{ $counterdata->id }}" name="">

                                    <div >
                                        <div >
                                            <label ></label>
                                            <div >
                                                <div >
                                                    <input name="counter_tittle_1"  type="text"  value="{{ $counterdata->counter_tittle_1 }}">
                                                </div>
                                                 <div >
                                                    <input name="counter_1" type="text"  value="{{ $counterdata->counter_1 }}">
                                                </div>
                                                <!-- <input  type="file"> -->

                                    
                                            </div>
                                        </div>
                                        
                                    </div>
                                    <div >
                                        <div >
                                            <label ></label>
                                            <div >
                                                <div >
                                                    <input name="counter_tittle_2" type="text"  value="{{ $counterdata->counter_tittle_2 }}">
                                                </div>
                                                 <div >
                                                    <input name="counter_2"  type="text"  value="{{ $counterdata->counter_2 }}">
                                                </div>
                                                <!-- <input  type="file"> -->

                                    
                                            </div>
                                        </div>
                                        
                                    </div>
                                    <div >
                                        <div >
                                            <label ></label>
                                            <div >
                                                <div >
                                                    <input name="counter_tittle_3" type="text"  value="{{ $counterdata->counter_tittle_3 }}" >
                                                </div> 
                                                 <div >
                                                    <input name="counter_3" type="text"  value="{{ $counterdata->counter_3 }}">
                                                </div>
                                                <!-- <input  type="file"> -->

                                    
                                            </div>
                                        </div>
                                        
                                    </div>
                                    <div >
                                        <div >
                                            <label ></label>
                                            <div >
                                                <div >
                                                    <input name="counter_tittle_4" type="text"  value="{{ $counterdata->counter_tittle_1 }}">
                                                </div>
                                                 <div >
                                                    <input name="counter_4"  type="text"  value="{{ $counterdata->counter_4 }}">
                                                </div>
                                                <!-- <input  type="file"> -->

                                    
                                            </div>
                                        </div>
                                        <center>
                                         <button  type="submit">Button</button>
                                         </center>
                                        
                                    </div>
                                   
                                    </form>
public function UpdateCounter(Request $request) {
        $counter_id = $request->id;

        AboutCounter::findOrFail($counter_id)->update([

            'counter_tittle_1' =>$request->counter_tittle_1,
            'counter_1' =>$request->counter_2,
            'counter_tittle_2' =>$request->counter_tittle_2,
            'counter_2' =>$request->counter_2,
            'counter_tittle_3' =>$request->counter_tittle_3,
            'counter_3' =>$request->counter_3,
            'counter_tittle_4' =>$request->counter_tittle_4,
            'counter_4'  =>$request->counter_4,
        ]);

        $notification = array(
            'message' => 'About Counter Updated ', 
            'alert-type' => 'success'
        );

        return redirect()->back()->with($notification);
    }

migrate table

public function up()
    {
        Schema::create('about_counters', function (Blueprint $table) {
            $table->id();
            $table->string('counter_tittle_1')->nullable();
            $table->string('counter_1')->nullable();
            $table->string('counter_tittle_2')->nullable();
            $table->string('counter_2')->nullable();
            $table->string('counter_tittle_3')->nullable();
            $table->string('counter_3')->nullable();
            $table->string('counter_tittle_4')->nullable();
            $table->string('counter_4')->nullable();
            $table->timestamps();
        });
    }

Thank You

CodePudding user response:

You can remove the comma and cast it to int like this:

{{ (int) str_replace(',', '', $number) }}
  • Related