markdown Laravel提示和技巧
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Laravel提示和技巧相关的知识,希望对你有一定的参考价值。
## ELOQUENT
### Available Parameters
```PHP
class User extends Model {
protected $table = 'users';
protected $fillable = ['email', 'password']; // which fields can be filled with User::create()
protected $dates = ['created_at', 'deleted_at']; // which fields will be Carbon-ized
protected $appends = ['field1', 'field2']; // additional values returned in JSON
protected $primaryKey = 'uuid'; // it doesn't have to be "id"
public $incrementing = false; // and it doesn't even have to be auto-incrementing!
protected $perPage = 25; // Yes, you can override pagination count PER MODEL (default 15)
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at'; // Yes, even those names can be overridden
public $timestamps = false; // or even not used at all
}
```
### Automatic Model Validation
```PHP
class Post extends Eloquent
{
public staic $autoValidates = true;
protected static $rules = [];
protected static function boot()
{
parent::boot();
// or static::creating, or static::updating
static::saving(function($model)
{
if ($model::$autoValidates) {
return $model->validate();
}
});
}
public function validate()
{
}
}
```
### UUID Model Primary Key
```PHP
use Ramsey\Uuid\Uuid;
trait UUIDModel
{
public $incrementing = false;
protected static function boot()
{
parent::boot();
static::creating(function ($model)
{
$key = $model->getKeyName();
if (empty($model->{$key})) {
$model->{$key} = (string) $model->generateNewUuid();
}
});
}
public function generateNewUuid()
{
return Uuid::uuid4();
}
}
```
### Simple Incrementing & Decrementing
```PHP
Article::find($article_id)->increment('read_count');
Article::find($article_id)->increment('read_count', 10); // +10
Product::find($produce_id)->decrement('stock'); // -1
```
### Appending Mutated Properties
```PHP
function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
class User extends Model
{
protected $appends = ['full_name'];
}
```
### 5. Simple Date Filtering
```PHP
User::whereDate('created_at', date('Y-m-d'));
User::whereDay('created_at', date('d'));
User::whereMonth('created_at', date('m'));
User::whereYear('created_at', date('Y'));
```
### Find Multiple Entries
```PHP
$users = User::find([1,2,3]);
```
### Eloquent::when() – no more if-else’s
```PHP
$query = Author::query();
$query->when(request('filter_by') == 'likes', function ($q) {
return $q->where('likes', '>', request('likes_amount', 0));
});
$query->when(request('filter_by') == 'date', function ($q) {
return $q->orderBy('created_at', request('ordering_rule', 'desc'));
});
```
### BelongsTo Default Model
```PHP
public function author()
{
return $this->belongsTo('App\Author')->withDefault([
'name' => 'Guest Author'
]);
}
```
### Order Mutator
```PHP
$clients = Client::get()->sortBy('full_name');
```
### Right Way Using Where and OrWhere
```PHP
$q->where(function ($query) {
$query->where('gender', 'Male')
->where('age', '>=', 18);
})->orWhere(function($query) {
$query->where('gender', 'Female')
->where('age', '>=', 65);
})
```
以上是关于markdown Laravel提示和技巧的主要内容,如果未能解决你的问题,请参考以下文章