博客列表 >12月30日larave 增删改查

12月30日larave 增删改查

随风
随风原创
2020年01月02日 14:31:13704浏览

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()
{

  1. $data = DB::select('select * from article where article_id =? ',[10]);
  2. foreach ($data as $k => $v){
  3. $data[$k]=(array)$v;
  4. }
  5. $data =$data[0]
  6. return view ('article/article',['res'=>$data]);
  7. }
  8. public function sel1()
  9. {
  10. $data = DB::table('article')->where('article_id',10)->get()->toArray();
  11. return view('article.article1',['res'=>$data]);
  12. }
  13. public function sel3()
  14. {
  15. $data = DB::table('article')->get()->toArray();
  16. return view('article.article3',['res'=>$data]);
  17. }
  18. public function myadd()
  19. {
  20. $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’]);

  1. print_r($data);
  2. return view('article.article',['res'=>$data]);
  3. }
  4. //修改 原生
  5. public function upd()
  6. {
  7. $data =DB::update("update article set title='原生修改数据' where article_id=29");

//
print_r($data);

  1. return view('article.article',['res'=>$data]);
  2. }

// 修改 构造器
public function upd1()
{
$data =DB::table(‘article’)
->where(‘article_id’,31)
// ->update([‘title’=>’构造器修改数据’]);
->update([‘title’=>’构造器修改数据’,’detail’=>’构造器修改数据’]);

  1. print_r($data);
  2. return view('article.article',['res'=>$data]);
  3. }
  4. // 删除 原生
  5. public function mydel()
  6. {
  7. $data = DB::delete('delete from article where article_id=33');
  8. print_r($data);
  9. return view('article.article', ['res' => $data]);
  10. }

// 删除 构造器
public function mydel1()
{
$data =DB::table(‘article’)
->where(‘article_id’,’34’)
->delete();
print_r($data);

  1. return view('article.article', ['res' => $data]);
  2. }

}`

article3
`
@foreach($res as $v)

  1. @if($v->article_id < 5)
  2. {{$v->title}}.买.<br>
  3. @elseif($v->article_id>5 && $v->article_id<10)
  4. {{$v->title}}.可以买.<br>
  5. @else
  6. {{$v->title}}.不能买.<br>
  7. @endif`
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议