DEV Community

S. M. Ahad Ali Chowdhury
S. M. Ahad Ali Chowdhury

Posted on • Edited on

2 1

laravel visitor details catch

Suppose you want to save visitor details who visit your website...
You have to do something for this
Step No-1:
go to code editor terminal and type
php artisan make:Migration (your table name)
Then go to migration file which you make just now
then code something in up function
Here is some example

Schema::create("your table name", function (Blueprint $table){
//$table->your data type here("column name");
$table->bigIncrements("id");
$table->string("ip_address");
$table->string("visit_time");
$table->string("visit_date");
}
go to code editor terminal again and type
php artisan migrate

step 1 done

step No-2
create model
php artisan make:Model (your model name)

**

class VisitorModel extends Model
{
public $table='Visitor';
public $primaryKey='id';
protected $fillable = ['ip_address', 'visit_time'];
public $incrementing=true;
public $keyType='int';
public $timestamps=false;
};
**

step 2 done

step No-3

create controller
php artisan make:Controller (your Controller name)
after make controller,
in this controller import visitormodel for inserting data in database . Like
use App\Models\VisitorModel;

and then
**

class VisitorController extends Controller
{
function getVisitorDetails(){
$ip_address = $_SERVER['REMOTE_ADDR'];
date_default_timezone_set("Asia/Dhaka");
$visit_time = date("h:i:sa");
$visit_date= date("d-m-Y");

  $result = VisitorMode::insert([
        "ip_address"=> $ip_address,
        "visit_time"=> $visit_time,
         "visit_date"=> $visit_date,
 ]);

        return $result;

}
}
**

step 3 done

step No-4
this data use only for client site. that's why we go to the route>api.php and than
create new route
Route::get("/getvisitordetails", {VisitorController::class, 'getVisitorDetails'});
and also you should import the visitorcontroller class path in top of the api.php.

for this use App\Http\Controllers\VisitorController;

step 4 done

step No-5
test the route
go to postman and test this route.

step 5 is done......

Step No_6:
If you want to use this api in your website. Just go to the your interface code and create src>api>apiURL.js
and then, create a class like this...

class url{
static baseURL = "your base url",
static visitorData = this.baseURL + "your visitor route"
}
export default url;

Now, go to your Homepage and write a function like

GetVisitorDetails=()=>{
axios.get(url.visitorData).then().catch();
}

and now call this function anywhere from the homepage.

that's it.

thank you

if you find any error please comment below in this post, I will try my best to fix it

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️