Home  >  Article  >  PHP Framework  >  Share a guide to writing suggestions in Laravel

Share a guide to writing suggestions in Laravel

藏色散人
藏色散人forward
2021-05-07 09:12:411659browse

Below, the laravel tutorial column will share with you a guide on how to write suggestions for Laravel. I hope it will be helpful to friends who need it!

Laravel is an elegant framework that also provides a lot of flexible and magical writing methods. But the more flexible it is, there may be minor problems in some aspects.

The following is a list of writing methods that Laravel does not recommend, for discussion and reference only. If possible, please share your opinion in the comments section.

Request problem

Request parameters can be obtained through dynamic attributes, such as the following writing:

$name = $request->name;

This writing method is not recommended because if the attribute name If it is an attribute of the original class such as query or content, unexpected effects will occur. Unless you memorize all the keywords of this type (even if you remember all the attributes, you can't guarantee whether new attributes will be added in the next version), otherwise you will encounter this pit sooner or later. It is not recommended to use it. It is recommended to use the following method:

// 可以用这种方式
$name = $request->input('name');
// 或用这种方式
$input = $request->all();
$name = $input['name'];

Summary: Do not use the request dynamic attribute.

Model appends

class User extends Model
{
  protected $appends = ['is_adult'];
  public function getIsAdultAttribute()
  {
      return $this->attribute['age'] > 18;
  }
}

getXxxAttribute is a very useful feature, but if append is automatically appended in the model, it will cause many problems, such as when you select , there is no age field, but the append field will still be added for you, which sometimes causes a lot of problems. It is recommended not to use the $append attribute. Load this property at the controller layer when needed.

// model
class User extends Model
{
  public function getIsAdultAttribute()
  {
      return $this->attribute['age'] > 18;
  }
}
// controller
$user = User::first();
$user->append('is_adult');

Summary: Do not use model appends.

Model's dynamic properties

// 第1种写法
$user = User::first();
// ?? 此动态属性如果是表字段名称是如 exists 等就会出问题。
$name = $user->name;
// 第2种写法更安全
$user = User::first()->toArray();
$name = $user['name'];

Model also has dynamic properties when using objects, which will also cause conflict problems (generally unlikely to be triggered), but the data table Keywords should not appear in the field as much as possible. (Although the second way of writing is safer, I believe more people prefer the first way of writing. We can just avoid the following keywords in database fields. I also like to use the first way of writing.)

// /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php 
exists,
incrementing
timestamps
wasRecentlyCreated

This article will be updated from time to time. I hope people who use Laravel can share your usage suggestions.

The above is the detailed content of Share a guide to writing suggestions in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete