今天發生強列地震,希望各位安好!
首先要先設定環境,env可以設定連線的相關資訊,雖然database.php可以將甚至是多個資料庫的連線資訊寫在裡面但一般會建議使用.env設定會比較安全。
以下是SQL寫法的相關範例。
    <?php
    
       //查詢
       $users = DB::select('select * from users where active = ?', [1]);
       $results = DB::select('select * from users where id = :id', ['id' => 1]);
       
       // 如果想要取得類似下拉式的資料可以使用以下寫法
       $burgers = DB::scalar("select count(case when food = 'burger' then 1 end) as burgers from menu");
       
       //新增
       DB::insert('insert into users (id, name) values (?, ?)', [1, 'Marc']);
       
       //更新
       $affected = DB::update('update users set votes = 100 where name = ?',['Anita']);
       
       //刪除
       $deleted = DB::delete('delete from users');
       
       //不須回傳值可以參考使用以下的寫法
       DB::statement('drop table users');
       
       //執行未預期的query可以使用以下寫法,盡量不要讓使用者可以輸入來控制此query以避免發生注入攻擊
       DB::unprepared('update users set votes = 100 where name = "Dries"' );
       
       //使用其他資料庫連線
       $users = DB::connection('sqlite')->select(/* ... */);
    ?>