<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| 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('index', config('settings'));
});

Route::get('/login', function () {
    if(session('user')){
        return view('admin/index', config('settings'));
    }
    return view('login', config('settings'));
});

Route::post('/login', [UserController::class, 'checkUser']);

Route::post('/admin/post', [PostController::class, 'insertPost']);

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class PostController extends Controller
{
    public function insertPost(Request $request){
        $id = Str::uuid()->toString();
        $title = $request->input('title');
        $content = $request->input('content');
        $thumb = $request->input('thumb');
        $datetime = $request->input('datetime');
        
        DB::insert('insert into posts (id, title, content, thumb, created_at) values (?, ?, ?, ?, ?)', [$id, $title, $content, $thumb, $datetime]);
    }

    public function selectPosts(){
        $posts = DB::select('select * from posts limit 7');
        return $posts;
    }
}

 

GitHub: https://github.com/Sokhavuth/laravel

Heroku: http://khmerweb-laravel.herokuapp.com/