larave 增删改查
web
Route::get('/sel','Article@sel');
Route::get('/sel1','Article@sel1');
Route::get('/sel2','Article@sel2');
Route::get('/sel3','Article@sel3');
Route::get('/myadd','Article@myadd');
Route::get('/myadd1','Article@myadd1');
Route::get('/upd','Article@upd');
Route::get('/upd1','Article@upd1');
Route::get('/mydel','Article@mydel');
Route::get('/mydel1','Article@mydel1');
Article
`namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
// 数据库增、删、查、改。
class Article extends Controller
{
//查 原生
public function sel()
{
$data = DB::select('select * from article where article_id =? ',[10]);
foreach ($data as $k => $v){
$data[$k]=(array)$v;
}
$data =$data[0] ;
return view ('article/article',['res'=>$data]);
}
public function sel1()
{
$data = DB::table('article')->where('article_id',10)->get()->toArray();
return view('article.article1',['res'=>$data]);
}
public function sel3()
{
$data = DB::table('article')->get()->toArray();
return view('article.article3',['res'=>$data]);
}
public function myadd()
{
$data =DB::insert("insert into article(title,fb_date,soure,ydl,pls,image,detail,cate_id)
values(‘原生插入数据’,’2019-12-31’,’北京’,’11’,’22’,’a.jpg’,’原生插入数据’,’2’)”);
print_r($data);
return view(‘article.article’,[‘res’=>$data]);
}
// 插入 构造器
public function myadd1()
{
$data = DB::table(‘article’)
->insert([‘title’=>’构造器插入数据’,’fb_date’=>’2019-12-31’,
‘soure’=>’北京’,’ydl’=>’22’,’pls’=>’33’,’image’=>’b.gif’,
‘detail’=>’构造器插入数据’,’cate_id’=>’3’]);
print_r($data);
return view('article.article',['res'=>$data]);
}
//修改 原生
public function upd()
{
$data =DB::update("update article set title='原生修改数据' where article_id=29");
//
print_r($data);
return view('article.article',['res'=>$data]);
}
// 修改 构造器
public function upd1()
{
$data =DB::table(‘article’)
->where(‘article_id’,31)
// ->update([‘title’=>’构造器修改数据’]);
->update([‘title’=>’构造器修改数据’,’detail’=>’构造器修改数据’]);
print_r($data);
return view('article.article',['res'=>$data]);
}
// 删除 原生
public function mydel()
{
$data = DB::delete('delete from article where article_id=33');
print_r($data);
return view('article.article', ['res' => $data]);
}
// 删除 构造器
public function mydel1()
{
$data =DB::table(‘article’)
->where(‘article_id’,’34’)
->delete();
print_r($data);
return view('article.article', ['res' => $data]);
}
}`
article3
`
@foreach($res as $v)
@if($v->article_id < 5)
{{$v->title}}.买.<br>
@elseif($v->article_id>5 && $v->article_id<10)
{{$v->title}}.可以买.<br>
@else
{{$v->title}}.不能买.<br>
@endif`