Home  >  Article  >  php教程  >  Detailed explanation of rare methods in laravel framework

Detailed explanation of rare methods in laravel framework

WBOY
WBOYOriginal
2016-09-19 08:55:011140browse

1.whereDate() method

<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'));

In the past, when checking data, I directly used the where condition to compare the values, but the format will have strict requirements. If the third parameter of the above code is date('Y-m-d') without adding the following 00:00:00 In this way, it will not be found in the database

And judging whether they are equal also has strict requirements on the format

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

Now use whereDate, laravel's own method, which will automatically help you process the date format to ensure that the date matches the search format.

<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'));

For example, the above code only searches when the number of days is a certain value. If you use whereDay, it will search the database for a matching value on a certain day.

2.wherePivot() method

Generally, the activity scene of this method is in the model

class Company extends \Eloquent{...
<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>

What does wherePivot mean? , first is the company and user tables, establishing a multi-association relationship, the association table is user_company_applies

First of all, if you don’t add wherePivot, how to display it

The database looks like this

Controller:

Display 3 pieces of data,

That is, find the user_id data corresponding to company_id 1 from the intermediate table, and then bring the user_id value to the user table to query what data there is, that is, these 3 items. This is the original many-to-many process

Now the wherePivot method is added, it is nothing more than adding a judgment condition. After obtaining the many-to-many data, then search for the apply_status and type fields in the intermediate table to find which piece of data is 1, and then Show

The database now looks like this

Both the two values ​​​​are 1. Let’s see if we can query the data with user_id 2525

Start testing

OK

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn