美文网首页
Model方法

Model方法

作者: jacklin1992 | 来源:发表于2016-09-29 21:23 被阅读280次

    基本方法

    查找

    Model::find(1);查找第一条
    Model::where('username','jacklin')->get();
    Model::where('id','>','1')->get();
    Model::get();
    Model::all();
    

    增加

    $id = 1;$username = 'jacklin';Model::save();
    $data = ['id'=>1,'username'=>'jacklin'],  Model::fill($data),Model::save();
    Model::create($data);//数组
    

    修改:

    $user = Model::find(4);
    $user -> username = 'jacklin2';
    Model::save();
    Model::where('id',$id)->update($data);//数组
    

    删除

    Model::where('id',$id)->delete();
    

    一个实例

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Http\Model\Goods;
    use Illuminate\Http\Request;
    
    use App\Http\Requests;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Input;
    use Illuminate\Support\Facades\Session;
    
    class SoaController1 extends Controller
    {
        public function data()
        {
            $data = DB::table('goods')->get();
            $json = json_encode($data);
            return $json;
        }
        //get pros
        public function index()
        {
            $data = json_decode($this->data());
            return view('admin/list1')->with('data',$data);
        }
        //get pros/create
        public function create()
        {
            return view('admin/add');
        }
    
        //post pros  store
        public function store()
        {
    
        $input = Input::except('_token');
        $re = Goods::create($input);
        if($re){
            return redirect('pros');
        }else{
            return back()->withErrors('添加失败');
        }
    }
        // get pros/{pros} show
    public function show()
    {
        echo 'show';
    }
    // get pros/{pros}/edit
    public function edit($id)
    {
        $res=DB::select('select * from goods where id = ?', array($id));
        $data = $res[0];
        return view('admin/update')->with('data',$data);
    }
    // put pros/{pros}
    public function update($id)
    {
        $input = Input::except('_token','_method');
    //        dd($input);
        $re = Goods::where('id',$id)->update($input);
        echo $re;
        if($re){
            return redirect('pros');
        }else{
            return back()->with('errors','信息更新失败');
        }
    
    }
    //delete pros/pros
    public function delete($id)
    {
        $re = Goods::where('id',$id)->delete();
        if($re){
            return redirect('pros');
        }else{
            echo "删除失败";
        }
    }
    
    }
    

    相关文章

      网友评论

          本文标题:Model方法

          本文链接:https://www.haomeiwen.com/subject/xyllyttx.html