laravel model层建立(以及控制器调用)

   我们编程一般都是采用mvc的形式,这个想法对我们来说根深蒂固,当我们了解lavarel框架的时候已经被它优雅的编程所折服,那么刚开始我们都会有疑问laravel的model层文件去哪里了,当我们使用PHP artisan make:model privilegeModel(名字随便写,可以不加Model) 创建model 时它默认放在了app同级目录下,也可以自己创建model,只要注意命名空间 就OK!

那么下面我们就开始在创建的model层中开发吧!

        创建好model层我们可以使用两种方式

       方式一:简单版   使用DB类操作  可以使用自己编写的任意方法

    model 层
[php]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. namespace App;  
  3. use Illuminate\Database\Eloquent\Model;  
  4. use Illuminate\Support\Facades\DB;  
  5. class Country extends Model  
  6. {  
  7.     protected $table = 'country';  
  8.     public $timestamps = false;  
  9.     public function readCountry()//查  
  10.     {  
  11.         return $this->all();  
  12.     }  
  13.     public function oneCountry($data,$arr)//单条查询  
  14.     {  
  15.         return $this->where($data,$arr)->get()->toArray();  
  16.     }  
  17.     public function delCountry($data)//删  
  18.     {  
  19.         $country = $this->where($data);  
  20.         return $country->delete();  
  21.     }  
  22.     public function updCountry($data,$list,$arr)//改  
  23.     {  
  24.         $country = $this->where($data,$list);  
  25.         return $country->update($arr);  
  26.     }  
  27.     public function addCountry($data)//增  
  28.     {  
  29.         return DB::table('country')->insert($data);  
  30.     }  
  31. }  




     

         控制器层加载model

  
[php]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2.   
  3. namespace App\Http\Controllers;  
  4. use App\Country;  
  5. use Illuminate\Http\Request;  
  6. use Illuminate\Database\Eloquent\Model;  
  7. use App\Http\Requests;  
  8. use Illuminate\Support\Facades\DB;  
  9. use Illuminate\Support\Facades\Session;  
  10. class Testcontroller extends Controller  
  11. {  
  12.     public function selectSome()  
  13.     {  
  14.         $country = new Country();  
  15.         $people = $country->readCountry();  
  16.         return view('test/sel',['people'=>$people]);  
  17.     }  
  18. }  


   

    DB类一般操作做:

      增加:DB::table('users')->insert(
                 array('email' => 'john@example.com', 'votes' => 0)
                 );

                建议添加使用   返回自增ID:

                $id = DB::table('users')->insertGetId(
                array('email' => 'john@example.com', 'votes' => 0)
                );

      删除:DB::table('users')->where('votes', '<', 100)->delete();  返回影响行数

      修改:DB::table('users')
                ->where('id', 1)
                ->update(array('votes' => 1));  返回影响行数

      查询:

              1、单条

                   $data= DB::table('blogs')->where('id',$id)->first();

              2、多条

                   $data= DB::table('blogs')->where("title","官人")->get();    ->where条件可以无限追加

   


      方式二:复杂版采用    Eloquent类(laravel受欢迎的主要原因)   不懂请看:官方文档:https://laravel-china.org/docs/5.1/eloquent-collections 

                                                                                                                   文章:https://lvwenhan.com/laravel/421.html

    控制器层加载model  直接User::(操作)

[php]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.   <?php  
  2.        namespace App\Http\Controllers;  
  3.        use Illuminate\Http\Request;  
  4.        use Illuminate\Support\Facades\DB;  
  5.        use Illuminate\Support\Facades\Input;  
  6.        use Illuminate\Support\Facades\Session;  
  7.        use App\Models\User;  
  8.   
  9. Class RunController extends Controller{  
  10.     public function add(Request $request){  
  11.            //判断接值  
  12.         if($request->isMethod('post')){  
  13.             //接值  
  14.             $data=Input::get();  
  15.             $username=$data['username'];  
  16.             $pwd=$data['pwd'];  
  17.          //获取全部  
  18.          $list1=User::get()->toArray();  
  19.          $res=User::where('username''=',$username )->where('pwd''='$pwd)->first()->toArray();  
  20.          // print_r($li);  
  21.         print_r($list1);  
  22.            
  23.    exit;  
  24.         }  
  25.         return view("admin/add");  
  26.     }  
  27. }  
   model层


[php]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?php   
  2. namespace App\Models;  
  3. use Illuminate\Database\Eloquent\Model;  
  4. use Illuminate\Support\Facades\DB;  
  5. class User extends Model{  
  6.     //设置表名  
  7.     const TABLE_NAME="register";  
  8.     protected $table = self::TABLE_NAME;  
  9.     public $timestamps = false;  
  10.    
  11. }  


一般操作:

      查询:get和all都是返回表中全部对象 加上toArra变成二维数组

              User::get()->toArray();

              User::all()->toArray();

              加上->first()  就是 获取第一条数据


注:转自woshihaiyong168的博客


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值