“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Recently, I have been updating articles on PHP, and the last few articles summarizing coding specifications and design thinking classes have also taken PHP as an example.

There are not many PHP articles, but you are still using PHP. , etc.

In the related recommendation at the bottom of my last few articles, I also read an article introducing PHP framework. The author of that article mainly uses TP framework, and I mainly use Laravel framework, so my code Demo and design ideas are more or less related to Laravel.

The advantage of PHP is that it is easy to learn, which leads to varying levels of quality among practitioners. I would like to add that it is easy to learn and there is no strong specification, which leads to beginner code dragging down PHP.

PHP is not as bad as it looks online. Of course, Go, Python, and Java do have some advantages over PHP, but PHP still has its place in the world with its simplicity and strong community.

The movie “Huo Yuanjia” : there is no superior martial arts in the world, only the martial arts have strong and weak.

The same is true of technology circles

I am still a supporter and promoter of PHP and Laravel in particular.

Eloquent Model use tips

query ()

  1. Using query() auto-completes methods in ides like PhpStorm
  2. Using Query () simplifies the code logic for a more elegant implementation

For example,

Do not use the query

 if($status && $type) {
 $users = User::where('status', $status)->where('type', $type)->latest()->get();
 } else if ($status) {
 $users = User::where('status', $status)->latest()->get(); 
 } else if ($type) {
 $users = User::where('status', $type)->latest()->get();
 } else {
 $users = User::latest()->get(); 
 }
Copy the code

Use the query

 $query = User::query();
 if ($status) {
  $query->where('status', $status);
 }
 if ($type) {
  $query->where('type', $type);
 } 
 $users = $query->latest()->get();
Copy the code

Reuse or clone query ()

The use of query is described above, but there is a special case to note: Sometimes you need to clone query()

Scenario: Get the active and inactive products created today

$query = Product::query(); $today = request()->date ?? today(); if($today){ $query->where('created_at', $today); $active_products = $query-> WHERE ('status', 1)->get(); $inactive_products = $query-> WHERE ('status', 0)->get(); // So we won't find any inactive products hereCopy the code

$inactive_products does not have the expected channel value because the value of $query changes with each execution. The above query statement into SQL is equivalent to

where status = 1 and startus = 2
Copy the code

To solve this problem, we can reuse the $Query object for multiple queries.

We can solve the problem by cloning the $query as follows:

$active_products = (clone $query)->where('status', 1)->get(); $query $inactive_products = (clone $query)-> WHERE ('status', 0)->get(); // So we'll get the inactive product from $queryCopy the code

Eloquent where date method

Eloquent, check the date using the whereDay(), whereMonth(), whereYear(), whereDate(), and whereTime() functions.

Very flexible and powerful Amway Carbon

$products = Product::whereDate('created_at', '2020-01-31')->get();

$products = Product::whereMonth('created_at', '11')->get();

$products = Product::whereDay('created_at', '28')->get();

$products = Product::whereYear('created_at', date('Y'))->get();

$products = Product::whereTime('created_at', '=', '19:23:18')->get();
Copy the code

Increment and decrement

If you want to increment a column in a table of the database, simply use the increment() function. You can not only add 1, you can also add some numbers, like 50.

Decrement () is used in the same way as the increment.

Post::find($post_id)->increment('view_count');

User::find($user_id)->increment('points', 50);

Post::find($post_id)->decrement('comment_count');

User::find($user_id)->decrement('fans_count', 50);
Copy the code

Define the timestamp column

If the database table does not contain timestamp fields creATED_AT and updated_AT, you can use the $TIMEstamps = false attribute to specify that the Eloquent model does not use them.

class Company extends Model
{
    public $timestamps = false;
}
Copy the code

You can also specify a TIMESTAMP field, replacing created_AT and updated_AT with other fields

class Company extends Model
{
    public $timestamps = true;
    
    const CREATED_AT = 'createtime';
    const UPDATED_AT = 'updatetime';
}
Copy the code

Model ALL: column

When invoking the Eloquent Model:: All (), you can specify which columns to return.

$users = User::all(['id', 'name', 'sex']);
Copy the code

Fail or not fail

In addition to findOrFail(), there is also the Eloquent firstOrFail() method, which returns page 404 if no query record is found.

$user = User::where('email', '[email protected]')->firstOrFail();
Copy the code

Welcome to the interactive

Welcome everyone to like attention, if you have any good suggestions, welcome to the comment section