1

I have a field in my database which has a datetime type column. I want that field to have today's date every time the view is loaded. Can I do this with AJAX and the location.reload() function?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Youssef Boudaya
  • 887
  • 2
  • 17
  • 29

2 Answers2

1

Here's some pseudo logic...

  1. You database field is likely to belong to a database table row.
  2. This database table row hopefully has a primary key.
  3. Each time your page loads, you get this primary key, and run an UPDATE query on the datetime field to now() corresponding to it.

Here's another resource with possible examples:

Update sql table whenever the page loads

suchislife
  • 4,251
  • 10
  • 47
  • 78
  • okay already began trying after your comment hope i'll get it right. i asked the question because i'm not excellent in developpement so i figured it will take me a long time to find the result thanks anyway. – Youssef Boudaya Mar 06 '18 at 10:23
  • I've added a link to a similar question here on stackoverflow containing examples for you as well. – suchislife Mar 06 '18 at 10:25
1

Ok first for ADVICE just before you hit ASK Question learning about how you searching about your problem then you wont see sally people says "learn how to ask" or "Vote Down"

second you should create function in your controller that make update the database time i use Carbon library its builtin in laravel and its very cool just import it

use Carbon\Carbon;

your controller function

public function UpdateDate(){
     $time=Carbon::now();
     $time->timezone = 'Europe/Athens';
     $UpdateNewDate=$time->format('Y-m-d');// choose the format you wanna
     //if you working with model so do model update by save()
     //if you dont use DB::update('');
     $InsertedDate=DB::update('update blablabla set ColumnName=?' where blabla=2,[$UpdateNewDate]');
    if($InsertedDate){
         return response()->json([
            'Status'=> true,
            'Message'=>'Updated',
            'Data' => null,
        ], 200);
     }
    else //check the error
}

then in Route web or api as you working on refere to your controller by post and i hope you know about auth you should make auth about any one inserted to DB or updated or select

Route::post('/UpdateDate','YourControllerName@UpdateDate');

in your blade view

 $.ajax({
    method: 'POST',
    dataType: 'json',
    url: "http://localhost/LaravelProject/public/UpdateDate",
    data: null,
    success: function(data){
    console.log('succes: '+data['Success']);
    }
});

then check your console by inspect if you see True then you are welcome if not contact me and you are welcome any way

mt5lesh 7d y2ll menk do your best ya sde2y :D

Ahmed Shams
  • 338
  • 1
  • 9