Blogger Information
Blog 19
fans 1
comment 0
visits 15272
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Laravel 增删改查 原生操作
海阔天空
Original
1113 people have browsed it

Laravel 增删改查 原生操作

Laravel支持PHP原生代码操作,一种熟悉的感觉

控制器 home.php<?phpnamespace App\Http\Controllers;use Illuminate\Support\Facades\DB;use Illuminate\Http\Request;class Home extends Controller{    public function index(){        $res=DB::select('select * from article ');        $lists=[];        foreach($res as $val){            $lists[]=(array)$val;        }        $data['result']=$lists;        // echo "<pre>";        // print_r($lists);        //view()第一个参数是视图文件,放在resources\views目录下        //文件名为test.blade.php.第二个参数为数组,传递数组的键名result        //在test.blade.php文件中,就可以使用$result        return view ('test',$data);        // return view ('test',['result'=>$lists]);    }    //数据库查询(原生查询)    public function test_select(){    //select * from article;    echo "<pre>";    $res=DB::select('select * from article where id>2');    print_r($res);    }    //数据库更新操作【原生更新】    public function test_update(){        $res=DB::update('update article set title="我知道了。" where id=3');        var_dump($res);    }    //数据库插入操作【原生插入】    public function test_insert(){        $res=DB::insert('insert article (`title`) value ("Laravel,我来了!")');        var_dump($res);    }    //数据库删除操作【原生删除】    public function test_delete(){        $res=DB::delete('delete from article where id=3');        var_dump($res);    }    //高级数据库查询方法【链式调用】    public function finds(){        //select * from article where id=3        $res=DB::table('article')->where('id',4)->first();        //$res是对象        echo $res->id;        echo "<br>";        Echo $res->title;        //将$res转为数组        $res=(array)$res;        echo "<pre>";        print_r($res);        echo $res['id']."<br>".$res['title'];    }
路由 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 () {    echo date("Y-m-d H:i:s");    return view('welcome');});//原生查询路由Route::get('/dbselect','home@test_select');//显示所有数据库记录路由Route::get('/dbindex','home@index');//原生更新路由Route::get('/dbupdate','home@test_update');//原生插入路由Route::get('/dbinsert','home@test_insert');//原生删除路由Route::get('/dbdelete','home@test_delete');//链式调用Route::get('/dbfinds','home@finds');
视图   testobject.blade.php<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>数据库查询2</title>    <link rel="stylesheet" href="/layui/css/layui.css" />    <script src="/layui/layui.js"></script></head><body>    <!-- <?php     echo "<pre>";    print_r($result);?> -->    <table class="layui-table">        <thead>            <tr>                <td>ID</td>                <td>CATE_ID</td>                <td>标题</td>            </tr>        </thead>        <tbody>            <?php foreach($result as $val){?>            <tr>              <td><?php echo $val->id?></td>              <td><?php echo $val->cate_id?></td>              <td><?php echo $val->title?></td>            </tr>            <?php }?>        </tbody>    </table></body></html>

效果图如下:

总结:
在学好php的基础上,掌握Laravel框架原生语法相对容易。抽时间再复习复习前面学习的基础知识。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:框架有用,但原生也非常重要
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post