Home > Net >  How can I modify this code to write to Laravel controller
How can I modify this code to write to Laravel controller

Time:04-10

This PHP code :

<?php
    if(isset($_POST['uid'])) {
        $uid = $_POST["uid"];
        $sql = mysqli_query($dbconnect, "INSERT INTO tb_entry VALUES ('$uid')");
    }
?>

How can I write this code into Laravel controller?

CodePudding user response:

it should be like this

if ($request->has('uid')) {
    DB::table('tb_entry')->insert([
        'uuid' => $request->uuid
    ]);
}

CodePudding user response:

  1. create a controller: php artisan make:controller MyController
  2. add a method to the controller like public function test(Request $request) {}
  3. rewrite your code a little bit and pasted in:
public function test(Request $request) {

    if (isset($request->post('uid'))) {
        $uid = $request->post('uid');
        // Note: you can use laravel model instead plain mysqli query
        $sql = mysqli_query($dbconnect, "INSERT INTO tb_entry VALUES ('$uid')");
    }
}

  • Related