Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:插入操作的返回值, 与原生是有区别的, 要注意
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\View\View;
class Home extends Controller
{
// 查询
function mySele()
{
$data=DB::select('select * from article ');
return view('index',['data'=>$data]);
}
// 用查询构造器
function mySele2(){
$data=DB::table('article')->where('id','>','5')->get();
return view('index',['data'=>$data]);
}
//插入
function myErt(){
$data2=DB::insert ('insert into article (id,title) values(11,"新插入进来的") ');
return view('index',['data2'=>$data2]);
}
//用插入构造器
//插入一条记录
function myErt2()
{
$data2 = DB::table('article')->insert(['id' => 9, 'title' => '新来的']);
return view('index',['data2'=>$data2]);
}
//插入多条记录
function myErt3(){
$data2 = DB::table('article')->insert(
[ [ 'title' => '新来的'], ['title'=>'插入的第10条记录'], ['title'=>'插入的第11条记录'] ]
);
return view('index',['data2'=>$data2]);
}
//insertGetId只能插入一条记录
function myErt4(){
$data2 = DB::table('article')->insertGetId(['id' => 12, 'title' => '新来的']);
return view('index',['data2'=>$data2]);
}
//insertGetId插入多条记录
function myErt5(){
$data5 = DB::table('article')->insert([
[ 'title' => '新来的2'],
[ 'title' => '新来的3'],
['title'=>'插入的第15条记录'],
]);
return view('index',['data5'=>$data5]);
}
//更新
function myUp(){
$data3=DB::update('update article set title ="我是行进过update过的" where id > 8');
return view('index',['data3'=>$data3]);
}
///用构造器更新
function myUp2(){
$data3=DB::table('article')->where('id',9)->update(['title'=>'uadate更新来的']);
return view('index',['data3'=>$data3]);
}
// 删除
function myDe(){
$data4=DB::delete('delete from article where id>8 ');
return view('index',['data4'=>$data4]);
}
///用构造器删除
function myDe2(){
$data4=DB::table('article')->where('id','>','8')->delete();
return view('index',['data4'=>$data4]);
}
//whereIn
function myWhere(){
$data5 = DB::table('article')->whereIn('id', [1, 2, 3])->get()->toArray();
return view('index',['data5'=>$data5]);
}
}
<?php
/*
|--------------------------------------------------------------------------
| 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('/show','Home@mySele');
Route::get('/show2','Home@mySele2');
Route::get('/myinsert','Home@myErt');
Route::get('/myinsert2','Home@myErt2');
Route::get('/myinsert3','Home@myErt3');
Route::get('/myinsert4','Home@myErt4');
Route::get('/myinsert5','Home@myErt5');
Route::get('/myUp','Home@myUp');
Route::get('/myUp2','Home@myUp2');
Route::get('/myDe','Home@myDe');
Route::get('/myDe2','Home@myDe2');
Route::get('/myWhere','Home@myWhere');
<div>
<p>输出如果id>3就输出title否者就输出id</p>
@foreach($data as $id)
@if($id->id>3)
<p>{{$id->id}}</p>
@else
<p>{{$id->title}}</p>
@endif
@endforeach
{{var_dump($data5)}}
{{var_dump($data4)}}
{{var_dump($data3)}}
{{var_dump($data2)}}
{{var_dump($data1)}}
</div>