一、使用composer安装laravel
create-project laravel/laravel mirrorapp
安装后的文件夹效果:
二、Laravel数据库原生的增删改查
Laravel中的MVC框架:
M:larevel没有模型目录,默认把 Eloquent 的模型放在 app 目录下,并允许开发人员将它们放在其他地方。V: larevel/resources/test.blade.phpC:larevel/App/Http/Controllers
在larevel/App/Http/Controllers创建Home.php文件
<?php//1. 命名空间:和目录一一对应//2. 类名称和文件名称一致namespace App\Http\Controllers;use Illuminate\Support\Facades\DB;//导入数据库class Home extends Controller{public function index(){// return 'www.php.cn';$time = date('Y-m-d H:i:s');//打印二维数组$res = DB::select('select * from article');$lists = [];foreach($res as $val){$lists[] = (Array)$val;}$data['result'] = $lists;echo '<pre>';print_r($data);return view('test',$data);;}//数据库查询[原生查询]public function get(){//select * from articleecho '<pre>';$res = DB::select('select * from article where id>2');print_r($res);// return view('test',$data);}//数据库更新操作[原生更新]public function test_updateadafd(){$res = DB::update('update article set title="我知道,最好的语言:php,25岁了!" where id=2');var_dump($res);}//新增数据[原生新增]public function insert_mytest(){$res = DB::insert('insert article(`id`,`title`)values(5,"excel鼠标滚轮上下失灵")');var_dump($res);}//删除数据【原生删除】public function delete_asdf(){$res = DB::delete('delete from article where id=2');var_dump($res);}}
在larevel/routes下打开web.php文件设置路由
<?phpuse Illuminate\Support\Facades\Route;/*|--------------------------------------------------------------------------| 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'); //视频 //视频引擎return view('test');});Route::get('/Home/index','Home@index');Route::get('/admins/article/lists','admins\Article@lists');Route::get('/dbselect','Home@get');Route::get('/dbupdate','Home@test_updateadafd');Route::get('/dbinsert','Home@insert_mytest');Route::get('/dbdelete','Home@delete_asdf');
输出效果:

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号