Sunday, April 9, 2023

[phpsandbox][laravel][web] quick setup laravel web service

 

(0) Create a phpsandbox account

Surf to https://phpsandbox.io

Create an account e.g. using Google account. 


(1) Create PHP Laravel Project

Select Laravel version e.g. Laravel 8

Type a name e.g. lara8


(2) Configure Database

In config/database.php edit as follows:

    |--------------------------------------------------------------------------

    | Default Database Connection Name

    |--------------------------------------------------------------------------

        'default' => env('DB_CONNECTION', 'sqlite'),

    |--------------------------------------------------------------------------

    | Database Connections

    |--------------------------------------------------------------------------

        'sqlite' => [

            'driver' => 'sqlite',

            'url' => env('DATABASE_URL'),

            'database' => database_path('database.sqlite'),

            'prefix' => '',

            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),

        ],


(3) Edit web script file

(In route/web.php)


<?php


use Illuminate\Support\Facades\Route;

use Illuminate\Support\Facades\DB;

use Illuminate\Database\Schema\Blueprint;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/


Route::get('/', function () {

    return view('welcome');

});

Route::get('/init', function () {

    Schema::dropIfExists('student');

    if (!Schema::hasTable('student')) {

        Schema::create('student', function (Blueprint $table) {

            $table->id();

            $table->string('name');

            $table->string('email')->unique();

        });

    }

    echo "init table.";

});

Route::get('/getall', function () {

    $student = DB::select('select * from student');

    echo "records:<br/>";      

    foreach($student as $a_student)

    {

        echo "record:".$a_student->id."|".$a_student->name."|".$a_student->email."<br/>";

    }  

});

Route::get('/insert/{name}/{email}',function($name,$email){

    $timestamp = time();

    DB::insert('insert into student (id, name,email) 

        values (?, ?,?)', 

        [$timestamp,$name.'_'.$timestamp,$email.'_'.$timestamp]);

    echo "record inserted.<br/>";

});

Route::get('/getone/{id}',function($id){ 

    $result = DB::select('select * from student where id = ?', [$id]);

    foreach($result as $a_result)

    {

        echo "record:<br/>".$a_result->id."|".$a_result->name."|".$a_result->email."<br/>";

    }  

});

Route::get('/update/{id}/{name}/{email}',function($id,$name,$email){

    $affected = DB::update(

    'update student set name = ?, email=? where id = ?',

    [$name,$email,$id]);

    echo "record updated:".$affected;

});

Route::get('/update1/{id}/{name}/{email}',function($id,$name,$email){

    $affected = DB::table('student')

      ->where('id',$id)

      ->update([

         'name'=>$name,

         'email'=>$email,

      ]);

    echo "record updated:".$affected;

});

Route::get('/delete/{id}',function($id){

    $deleted = DB::delete(

    'delete from student where id = ?',

    [$id]);

    echo "record deleted:".$deleted;

});

Route::get('/delete1/{id}',function($id){

    $deleted = DB::table('student')

    ->where('id', '=',$id)

    ->delete();

    echo "record deleted:".$deleted;

});

Route::get('/show_tables',function(){

    //$tables =  DB::select('SHOW TABLES'); 

    $tables = DB::select("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;");

    foreach($tables as $table){

        $arry =  (array) $table;

        foreach ($arry as $value) {

            echo $value."<br/>";

        }

    }

});



Example:

https://phpsandbox.io/n/delicate-grass-bdov-ai24b



.







No comments:

Post a Comment