Table des matières
1.whereDate() 方法
2.wherePivot() 方法
Maison php教程 php手册 laravel框架少见方法详解

laravel框架少见方法详解

Sep 19, 2016 am 08:55 AM

1.whereDate() 方法

<span style="color: #008080;">1</span> <span style="color: #800080;">$q</span>->where('created_at', '>=', <span style="color: #008080;">date</span>('Y-m-d').' 00:00:00'));
Copier après la connexion

以前查数据时,直接用where条件来比值判断,但是格式就会有严格的要求,如果上面的代码 第三个参数 是 date('Y-m-d') 而不加 后面的00:00:00 这样在数据库里面就会找不到

而判断是否相等 也都是对格式严格的要求

<span style="color: #800080;">$q</span>->whereDate('created_at', '=', <span style="color: #008080;">date</span>('Y-m-d'));
Copier après la connexion

现在用whereDate,laravel自带的方法,就会自动帮你进行日期格式处理,保证date与搜索的格式是匹配的。

<span style="color: #800080;">$q</span>->whereDay('created_at', '=', <span style="color: #008080;">date</span>('d'<span style="color: #000000;">));
</span><span style="color: #800080;">$q</span>->whereMonth('created_at', '=', <span style="color: #008080;">date</span>('m'<span style="color: #000000;">));
</span><span style="color: #800080;">$q</span>->whereYear('created_at', '=', <span style="color: #008080;">date</span>('Y'));
Copier après la connexion

例如上面的代码,只搜索天数为某值的时候,用whereDay,会去数据库里搜索某一天与其匹配的值。

 

 

2.wherePivot() 方法

一般这个方法的活动场景,模型中

class Company extends \Eloquent{...
Copier après la connexion
<br> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> memberApplies()
  {
    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->belongsToMany('App\User', 'user_company_applies'<span style="color: #000000;">)
      </span>->wherePivot('apply_status', UserCompanyApply::<span style="color: #000000;">applyVerify)
      </span>->wherePivot('type', UserCompanyApply::<span style="color: #000000;">TYPE_USER);
  }</span>
Copier après la connexion

wherePivot什么意思?,首先是company和user表,建立多关联的关系,关联表是user_company_applies

首先如果不加wherePivot,怎么显示

数据库这样

控制器:

显示3条数据,

也就是从中间表里找company_id为1对应的user_id的数据,在把user_id的值带到user表里去查询有哪些数据,也就是这3条,这是原来的多对多的流程

现在加上了wherePivot方法,无非是加上一个判断条件,获得多对多的数据之后,再去查找中间表里的apply_status和type字段,查找2个都为1的数据是那条,有则显示

数据库现在改成这样

2个值都为1了,看能不能查询出 user_id为2525的这条数据

开始测试

OK

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Vous avez un jeu croisé?
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)