Більшість з них, ймовірно, загальновідомі, але я записую блоки коду, які, як я знаю, я не буду використовувати часто і про які забуду. Час від часу я переглядаю їх, щоб освіжити пам'ять.
Ось 15 фрагментів / методів Laravel, які я знайшов, прокручуючи мій SnippetsLab:
1. Визначення того, новий
$product = Product::firstOrCreate(...)
if ($product->wasRecentlyCreated()) {
// New product
} else {
// Existing product
}
запис на firstOrCreate 2. Пошук пов'язаних ідентифікаторів у зв'язку
$user->roles()->allRelatedIds()->toArray();
BelongsTomany 3. abort_unless()
// Instead of
public function show($item) {
if (// User can not do this thing) {
return false;
}
// Else do this
}
// Do this
public function show($item) {
abort_unless(Gate::allows('do-thing', $item), 403);
// Actual logic
}
// Another use case, make sure the user is logged in
abort_unless(Auth::check(), 403);
4. Ключі
User::all()->pluck('id')->toArray();
// In most cases, however, this can be shortened. Like this:
User::all()->modelKeys();
моделі 5. throw_if()
throw_if(
!Hash::check($data['current_password'], $user->password),
new Exception(__('That is not your old password.'))
);
6. Скидання всіх стовпців таблиці
Schema::getColumnListing('table')
7. Перенаправлення на зовнішні домени
return redirect()->away('https://www.google.com');
8. Request exists() vs has()
// http://example.com?popular
$request->exists('popular') // true
$request->has('popular') // false
http://example.com?popular=foo
$request->exists('popular') // true
$request->has('popular') // true
9. @isset
// From
@if (isset($records))
// $records is defined and is not null
@endif
// To
@isset($records)
// $records is defined and is not null
@endisset
10. @empty
// From
@if (empty($records))
// $records is "empty"
@endif
// To
@empty($records)
// $records is "empty"
@endempty
11. @forelse
// From
@if ($users->count())
@foreach ($users as $user)
@endforeach
@else
@endif
// To
@forelse ($users as $user)
{{ $user->name }}
@empty
<p>No Users</p>
@endforelse
12. array_wrap()Помічник optional()
$posts = is_array($posts) ? $posts : [$posts];
// Same as
$posts = array_wrap($posts);
// Inline
foreach (array_wrap($posts) as $post) {
// ..
}
дозволяє отримати доступ до властивостей або методів виклику об'єкта. Якщо даний об'єкт нульовий, властивості і методи повернуть null замість того, щоб викликати помилку.
// User 1 exists, with account
$user1 = User::find(1);
$accountId = $user1->account->id; // 123
// User 2 exists, without account
$user2 = User::find(2);
$accountId = $user2->account->id; // PHP Error: Trying to get property of non-object
// Fix without optional()
$accountId = $user2->account ? $user2->account->id : null; // null
$accountId = $user2->account->id ?? null; // null
// Fix with optional()
$accountId = optional($user2->account)->id; // null
14. data_get()Помічник data_get()
дозволяє отримати значення з масиву або об'єкта з позначенням точок. Це також функціонує аналогічно array_get(). Необов'язковий третій параметр можна використовувати для надання значення за замовчуванням, якщо ключ не знайдено.
$array = ['albums' => ['rock' => ['count' => 75]]];
$count = data_get($array, 'albums.rock.count'); // 75
$avgCost = data_get($array, 'albums.rock.avg_cost', 0); // 0
$object->albums->rock->count = 75;
$count = data_get($object, 'albums.rock.count'); // 75
$avgCost = data_get($object, 'albums.rock.avg_cost', 0); // 0
15. push()
Зберегти модель та відповідні їй зв'язки можна за допомогою методу push().
$user = User::first();
$user->name = "Peter";
$user->phone->number = '1234567890';
$user->push(); // This will update both user and phone record in DB