1. Single behavior controller

/ / controller
    public function __invoke(){
        //
    }
/ / routing
Route::get('demo19'.'Test\TestController');  
Copy the code

2. Route rollback

// The route was not found to run
// Put it at the bottom
Route::fallback(function () {
    return redirect('/');
});
Copy the code

3. Route information

// Current routing information
dump(Route::current());
// Alias of the current route
echo Route::currentRouteName();
// The method to which the current route points
echo Route::currentRouteAction();
Copy the code

4. Resource controller

HTTP Request Mode URL Controller method Routing named Business logic description
GET post index() post.index Display all articles
GET post/create create() post.create Publish article Form page (API none)
POST post store() post.store Get the form submission data and save the new article
GET post/{id} show() post.show Display individual articles
GET post/{id}/edit edit() post.edit Edit article Form page (API none)
PUT post/{id} update() post.update Get the edit form input and update the article
DELETE post/{id} destroy() post.destroy Delete a single article
// php artisan make:controller UserController --resource
// php artisan make:controller UserController --api

// Single resource route
Route::resource('api'.'Test\ApiController');
// API resource routing
Route::apiResource('api'.'Test\ApiController');

// Batch resource routing
Route::resources([
    'api'= >'Test\ApiController'
]);
Copy the code

5. Divide the database into blocks

// Retrieve 100 operations at a time for 10,000 more data
// Update with where conditions will have pits
$result = DB::table('uinfo')
            ->orderBy('id')
            ->chunk(100.function ($result) {
                foreach ($result as $item) {
                    $item->username = $item->username . '# # # 22'; }});Copy the code

6. The database checks whether the data exists

/ / return true
DB::table('uinfo')->where('id'.19)->exists();
/ / returns false
DB::table('uinfo')->where('id'.19)->doesntExist()
Copy the code

7. Database select query

//1.addSelect
// Add more fields to the already built query
 $base = DB::table('uinfo')->select('username'.'password');
        $result = $base->addSelect('updated_at')->get();
//2.DB::raw()
//select() implements native expressions internally
$result = DB::table('uinfo')->select(DB::raw('count(*) as num'))
            ->groupBy('username')
            ->get();  
//3.selectRaw
//selectRaw() implements native expressions internally
$result=DB::table('uinfo')
            ->groupBy('username')
            ->selectRaw('count(*) as num,username')
            ->get();                
Copy the code

8. UpdateOrInsert is modified but not added

 $result = DB::table('uinfo')->updateOrInsert(
            ['id'= >15],
            ['username'= >'Test 1 of DEMO14'.'password'= >'123456'.'updated_at'= >$time.'created_at'= >$time]);Copy the code

9.Laravel adds code hints

composer requireBarryvdh /laravel-ide-helper PHP artisan ide-helper:generate -- generate annotations for Facades PHP artisan ide-helper:models -- generate annotations for data models PHP artisan IDE-Helper :meta - PhpStorm meta file is generatedCopy the code

10. Model scope: Local scope

// Use scope in front of the model
public function scopeCheckId($query.$index)
{
    return $query->where('id'.'>'.$index);
}
/ / controller
$result = Uinfo::checkId(5)->get();
Copy the code

11. Model scope: Global scope

/ / create the class
namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class NameScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        // TODO: Implement apply() method.

        return $builder->where('id'.'>'.6); }}//2. Enable global scope
protected static function boot()
{
    parent::boot(); // TODO: Change the autogenerated stub
    static::addGlobalScope(new NameScope());
}
Copy the code

Model accessor

// Before accessor :get: After field name :Attribute
public function getUsernameAttribute($value)
{
     return '[' . $value . '] ';
}
// Virtual field
protected $appends = ['info'];
public function getInfoAttribute()
{
     return '# #'.$this->password;
}
Copy the code

13. Model modifier

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = '&' . $value;
}
Copy the code

14. Model preloading with

/ / Ucard model
public function be_user()
{
    return $this->belongsTo(Uinfo::class, 'u_id'.'id');
}
/ / controller
$result = Ucard::with('be_user')->get();

$result = Ucard::with(['be_user'= >function ($query) {
    $query->where('id'.'>'.5);
}])->get();
Copy the code

15. Model association (one-to-many) addition, deletion and modification

/ / Uinfo model
public function userCard()
{
    return $this->Hasone(Ucard::class, 'u_id'.'id');
}
/ / controller
//save
$result = Uinfo::find(15)->userCard()->save(new Ucard(['card'= >'15646484']));
//create
Uinfo::find(15)->userCard()->create(['card'= >2525]);
//update
Uinfo::find(15)->userCard()->update(['card'= >'change']);
//delete
Uinfo::find(9)->userCard()->delete();
Copy the code

16. Model association (many-to-many) addition, deletion and modification

/ / model of the Article
public function rel_article()
{
    return $this->belongsToMany(Keyword::class, 'a_k'.'a_id'.'k_id');
}
/ / controller
$k_id = 6;
/ / the attach increasing a_id = 1, k_id = 6
Article::find(1)->rel_article()->attach($k_id['remarks'= >Many-to-many increment]);
//sync unique add does not exist add (will be deleted before)
Article::find(1)->rel_article()->sync($k_id);
/ / delete detach a_id = 1, k_id = 6
Article::find(1)->rel_article()->detach($k_id);
/ / updateExistingPivot modification
Article::find(1)->rel_article()->updateExistingPivot(3['remarks'= >'change']);
Copy the code