This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// meh | |
$mock | |
->method('sum') | |
->withConsecutive([1,2,3], [4,5,6]) | |
->willReturnOnConsecutiveCalls(6, 15); | |
// instead? | |
$mock->method('sum')->with(1,2,3)->willReturn(6); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Laravel 7.* | |
// Testing jobs: | |
Queue::later(now()->addMinute(), new SomeJob); | |
Queue::assertPushed(SomeJob::class, fn () => ...); // works | |
SomeJob::dispatch()->delay(now()->addMinute()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$this->builder | |
->select('*') // needs to be here, otherwise the only returned column will be `last_login` | |
->selectSub(function ($q) { | |
$q->from('action_logs') | |
->whereColumn('user_id', 'users.id') | |
->selectRaw('max(created_at)'); | |
}, 'last_login') | |
->get(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
root /your/root/path; | |
index index.html; | |
server_name you.server.com; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Schema::table('clicks', function (Blueprint $table) { | |
$table->index('user_id'); | |
$table->index(['user_id', 'date_clicked']); | |
$table->index(['user_id', 'date_clicked', 'ip']); | |
$table->index(['user_id', 'date_clicked', 'country']); | |
$table->index(['user_id', 'date_clicked', 'city']); | |
$table->index(['user_id', 'date_clicked', 'browser']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$mock = $this->createMock(SomeDependency::class); | |
$mock->expects($this->once())->method('firstMethod')->willReturn('something'); | |
$mock->expects($this->once())->method('secondMethod')->willReturn('something else'); | |
$cut = new SomeClass($mock) | |
$cut->act(); | |
// how to ensure there's no method called other than the 2 above? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function someController(Request $request) | |
{ | |
// asumming ValidatesRequests trait in use we'll call $this->validate | |
$this->validate($request, [ | |
// first set of rules | |
]); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ids = array_unique([1,2,3,4,5, ...]); | |
// relation() is HasMany | |
// check if all $ids belong to this parent model: | |
$parent_model->relation()->whereIn('id', $ids)->count() === count($ids) | |
// check if $ids match all of the children records: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// assuming you want to get conv where last message doesn't come from the OP | |
Coversation::where('author_id', '!=', function ($q) { | |
$q->select('author_id')->from('messages')->whereRaw('conversation_id = conversations.id')->latest()->limit(1); | |
})->get() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Do this (explicit routes are better): | |
Route::get('qualification', function () {/**/}); | |
Route::get('qualification/{year}/{month}', function ($year, $month) {/**/}); | |
// You could also do this (but don't in your case): | |
// @link https://laravel.com/docs/5.6/routing#parameters-regular-expression-constraints | |
Route::get('qualification/{year_month?}', function ($year_month = null) { |
NewerOlder