Guest User

Untitled

a guest
Feb 16th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. # #Order by Mutator
  2. >
  3. function getFullNameAttribute()
  4. {
  5. return $this->attributes['first_name'] . ' ' . $this->attributes['last_name'];
  6. }
  7.  
  8. $clients = Client::get()->sortBy('full_name'); // works!
  9.  
  10. # #Default ordering in global scope
  11. >
  12. protected static function boot()
  13. {
  14. parent::boot();
  15.  
  16. // Order by name ASC
  17. static::addGlobalScope('order', function (Builder $builder) {
  18. $builder->orderBy('name', 'asc');
  19. });
  20. }
  21.  
  22. # #truy vấn điều kiện where
  23. >
  24. # where name AND email
  25. \App\User::whereNameAndEmail('phanlyhuynh','lyhuynh@gmail.com')->first();
  26. # where name OR email
  27. \App\User::whereNameOrEmail('huynh','huynh@gmail.com')->get();
  28.  
  29. # #giá trị default cho relation
  30. >
  31. public function user()
  32. {
  33. return $this->belongsTo('App\User')->withDefault(function ($user) {
  34. $user->name = 'Guest Author';
  35. });
  36. }
  37.  
  38. # #cache dữ liệu
  39. $value = Cache::remember('users', $minutes, function () {
  40. return DB::table('users')->get();
  41. });
  42.  
  43. $value = Cache::rememberForever('users', function () {
  44. return DB::table('users')->get();
  45. });
  46.  
  47. # #Sử dụng fresh() để truy vấn database và lấy phiên bản mới của item hiện tại
  48. >
  49. $user = \App\User::first();
  50. $user->name = "Something new";
  51. $user = $user->fresh(); // chú ý rằng nó sẽ trả về giá trị mới, nó không ảnh hưởng tới model hiện tại
  52. dump($user->name); // nó sẽ trả về tên gốc, gốc phải "Something newm"
  53.  
  54. # #muốn rehydrate model đã tồn tại, chúng ta sử dụng refresh()
  55. >
  56. $flight = App\Flight::where('number', 'FR 999')->first();
  57. $flight->number = 'FR 111';
  58. $flight->refresh();
  59. $flight->number; // "FR 999"
  60.  
  61. # # chuyển hướng 301
  62. >
  63. Route::redirect('/here', '/there', 301);
  64.  
  65. # #truy vấn vào model và lấy cả relation của nó
  66. >
  67. $postComments = Post::with('comments)->get();
  68. $postComments = Post::with('comments')->has('comments')->get();
  69.  
  70. # #Increments and Decrements thuộc tính của model
  71. >
  72. $article = Article::find($article_id);
  73. $article->read_count++;
  74. $article->save();
  75.  
  76. #OR
  77. $article = Article::find($article_id);
  78. $article->increment('read_count');
  79.  
  80. #OR
  81. Article::find($article_id)->increment('read_count');
  82. Article::find($article_id)->increment('read_count', 10); // +10
  83. Product::find($produce_id)->decrement('stock'); // -1
  84.  
  85. # #truy vấn với điều kiện ngày tháng năm
  86. >
  87. User::whereDate('created_at', date('Y-m-d'));
  88. User::whereDay('created_at', date('d'));
  89. User::whereMonth('created_at', date('m'));
  90. User::whereYear('created_at', date('Y'));
  91.  
  92. # #Order by relationship
  93. >
  94. # first
  95. public function latestPost()
  96. {
  97. return $this->hasOne(\App\Post::class)->latest();
  98. }
  99. #seconds
  100. $users = Topic::with('latestPost')->get()->sortByDesc('latestPost.created_at');
  101. # #hạn chế sử dụng if else trong truy vấn
  102. >
  103. $query = Author::query();
  104. $query->when(request('filter_by') == 'likes', function ($q) {
  105. return $q->where('likes', '>', request('likes_amount', 0));
  106. });
  107. $query->when(request('filter_by') == 'date', function ($q) {
  108. return $q->orderBy('created_at', request('ordering_rule', 'desc'));
  109. });
  110.  
  111. # #điều kiện where, orwhere
  112. >
  113. $q->where(function ($query) {
  114. $query->where('gender', 'Male')
  115. ->where('age', '>=', 18);
  116. })->orWhere(function($query) {
  117. $query->where('gender', 'Female')
  118. ->where('age', '>=', 65);
  119. });
Add Comment
Please, Sign In to add comment