laravel 学习笔记------Eloquent

     在学习Eloquent之前首先要了解

php artisan make:model articles

     这时你会发现在  app/   目录下可找到文件  articles.php  

    (注:php artisan tinker 是 一个 REPL (read-eval-print-loop) ,REPL 是指 交互式命令行界面,它可以让你输入一段代码去执行,并把执行结果直接打印到命令行界面里 )

     我们现在开始插入第一条信息(本次实在artisan tinker中调试):

$articles=new App\articles;
$articles->title='first title';
$articles->content='first content';
$articles->published_at=Carbon\Carbon::now();
$articles->intro='first intro';

查看articles

>>> $articles
=> App\articles {#678
     title: "next",
     content: "content two",
     published_at: Carbon\Carbon {#685
       +"date": "2017-05-31 11:22:43.000000",
       +"timezone_type": 3,
       +"timezone": "PRC",
     },
   }
>>>

此时并没有将数据存储进数据库中,只是给article赋值,写入数据库:

>>> $articles->save();
=> true
>>>

我们可以更清晰的查看刚刚存储进数据库的信息:

>>> $articles->toArray();
=> [
     "title" => "next",
     "content" => "content two",
     "published_at" => Carbon\Carbon {#685
       +"date": "2017-05-31 11:22:43.000000",
       +"timezone_type": 3,
       +"timezone": "PRC",
     },
     "updated_at" => "2017-05-31 11:29:48",
     "created_at" => "2017-05-31 11:29:48",
     "intro" => "first intro",
     "id" => 2,
   ]
>>>

有了最简单的创建数据之后我们开始逻辑查找数据:

>>> $my=App\articles::find(2);
=> App\articles {#692
     id: 2,
     title: "next",
     content: "content two",
     published_at: "2017-05-31 11:22:43",
     created_at: "2017-05-31 11:29:48",
     updated_at: "2017-05-31 11:29:48",
     intro: "first intro",
   }
>>>

更新数据:

>>> $articles->title='update'
=> "update"
>>> $articles->save();
=> true
>>> $articles->find(2)
=> App\articles {#695
     id: 2,
     title: "update",
     content: "content two",
     published_at: "2017-05-31 11:36:59",
     created_at: "2017-05-31 11:29:48",
     updated_at: "2017-05-31 11:36:59",
     intro: "first intro",
   }
>>>

有时候会用到where查询:

>>> $where_result=App\articles::where('title','=','update')->get();
=> Illuminate\Database\Eloquent\Collection {#702
     all: [
       App\articles {#705
         id: 2,
         title: "update",
         content: "content two",
         published_at: "2017-05-31 11:36:59",
         created_at: "2017-05-31 11:29:48",
         updated_at: "2017-05-31 11:36:59",
         intro: "first intro",
       },
     ],
   }
>>>

 其中where('title','=','update')    '='可以省略,在laravel中为空默认添加'=';

也可以直接返回数据集:

>>> $where_result=App\articles::where('title','update')->first();
=> App\articles {#696
     id: 2,
     title: "update",
     content: "content two",
     published_at: "2017-05-31 11:36:59",
     created_at: "2017-05-31 11:29:48",
     updated_at: "2017-05-31 11:36:59",
     intro: "first intro",
   }
>>>

我们可以一次性写入数据:

>>> $articles=App\articles::create(['title'=>'third title','content'=>'third con
tent','published_at'=>Carbon\Carbon::now(),'intro'=>'third intro'])
Illuminate\Database\Eloquent\MassAssignmentException with message 'title'
>>>

这是后你会发现报错,这是因为laravel自身默认保护这些字段,不可以直接填充,解决方法:

 protected $fillable =  ['title','content','published_at','intro'];

然后重新进行写入(注:先退出tinker模式重新进入):

D:\phpStudy\WWW\laravel>php artisan tinker
Psy Shell v0.8.5 (PHP 5.6.27 鈥?cli) by Justin Hileman
>>> $articles=App\articles::create(['title'=>'third title','content'=>'third con
tent','published_at'=>Carbon\Carbon::now(),'intro'=>'third intro'])
=> App\articles {#693
     title: "third title",
     content: "third content",
     published_at: Carbon\Carbon {#689
       +"date": "2017-05-31 13:24:41.000000",
       +"timezone_type": 3,
       +"timezone": "PRC",
     },
     intro: "third intro",
     updated_at: "2017-05-31 13:24:41",
     created_at: "2017-05-31 13:24:41",
     id: 3,
   }
>>>

完美!!!!

数据更新:

>>> $articles->update(['title'=>'third ywp'])
=> true
>>>

create方法对我们来说很便利,一句话就可以搞定数据写入更改;

 

安装laravel表单扩展参考:https://laravelcollective.com/docs/master/html

 

setAttribute方法:预处理

public function setTittleAttribute($date){
    $this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d',$date);
}

quertscopef方法:

控制器中:

public function news(){
    DB::table('articles')->published()->get();
}

model:

public function scopePublished($query){
    $query->where('published_at','<=',Carbon::now());
}

carbon强大之处

例:

carbon对象->year; 就会显示出时间

carbon对象->diffForhumans();可显示当前时间与设置时间时长;

注:如果对象不是carbon对象时,可在model中设计 $dates=['对象'];

转载于:https://my.oschina.net/sunYwp/blog/911660

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值