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

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

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

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

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

    model 层
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Country extends Model
{
    protected $table = 'country';
    public $timestamps = false;
    public function readCountry()//查
    {
        return $this->all();
    }
    public function oneCountry($data,$arr)//单条查询
    {
        return $this->where($data,$arr)->get()->toArray();
    }
    public function delCountry($data)//删
    {
        $country = $this->where($data);
        return $country->delete();
    }
    public function updCountry($data,$list,$arr)//改
    {
        $country = $this->where($data,$list);
        return $country->update($arr);
    }
    public function addCountry($data)//增
    {
        return DB::table('country')->insert($data);
    }
}






     

         控制器层加载model

  
<?php

namespace App\Http\Controllers;
use App\Country;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class Testcontroller extends Controller
{
    public function selectSome()
    {
        $country = new Country();
        $people = $country->readCountry();
        return view('test/sel',['people'=>$people]);
    }
}


  

    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
       namespace App\Http\Controllers;
       use Illuminate\Http\Request;
       use Illuminate\Support\Facades\DB;
       use Illuminate\Support\Facades\Input;
       use Illuminate\Support\Facades\Session;
       use App\Models\User;

Class RunController extends Controller{
    public function add(Request $request){
           //判断接值
        if($request->isMethod('post')){
            //接值
            $data=Input::get();
            $username=$data['username'];
            $pwd=$data['pwd'];
         //获取全部
         $list1=User::get()->toArray();
         $res=User::where('username', '=',$username )->where('pwd', '=', $pwd)->first()->toArray();
         // print_r($li);
        print_r($list1);
         
   exit;
        }
        return view("admin/add");
    }
}
   model层


<?php 
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class User extends Model{
	//设置表名
	const TABLE_NAME="register";
	protected $table = self::TABLE_NAME;
    public $timestamps = false;
 
}


一般操作:

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

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

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

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








  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值