phpdocumentor/type-resolver 0.4.0 0.7.1
phpunit/php-code-coverage 6.1.4 7.0.3 Library that provides collection, processing, and rende...
phpunit/phpunit 7.5.9 8.1.3 The PHP Unit Testing framework.
ralouphie/getallheaders 2.0.5 3.0.3 A polyfill for getallheaders.
sebastian/global-state 2.0.0 3.0.0 Snapshotting of global state
// 2020-04-20 08:12:34echonow();// 2020-04-20 08:12:00echonow()->setSeconds(0);// 2020-04-20 08:00:00echonow()->setSeconds(0)->setMinutes(0);// Another way - even shorterechonow()->startOfHour();
$userCondition =function ($query) { $query->where('user_id',auth()->id());};// Get articles that have comments from this user// And return only those comments from this user$articles =Article::with(['comments'=> $userCondition])->whereHas('comments', $userCondition)->get();
// We have an array[ 0=> ['user_id'=>'some user id','created_at'=>'some timestamp','product'=> {object Product}, etc],1=> ['user_id'=>'some user id','created_at'=>'some timestamp','product'=> {object Product}, etc],2=> etc]// Now we want to get all products ids. We can do like this:data_get($yourArray,'*.product.id');// Now we have all products ids [1, 2, 3, 4, 5, etc...]
useApp\Console\Commands\SendEmailsCommand;$schedule->command('emails:send Taylor --force')->daily();$schedule->command(SendEmailsCommand::class, ['Taylor','--force'])->daily();
// First way ->with()returnview('index')->with('projects', $projects)->with('tasks', $tasks)// Second way - as an arrayreturnview('index', ['projects'=> $projects,'tasks'=> $tasks ]);// Third way - the same as second, but with variable$data = ['projects'=> $projects,'tasks'=> $tasks];returnview('index', $data);// Fourth way - the shortest - compact()returnview('index', compact('projects','tasks'));
classMigrationsTestextendsTestCase{publicfunctiontest_successful_foreign_key_in_migrations() {// We just test if the migrations succeeds or throws an exception$this->expectNotToPerformAssertions(); }}
Str 的 mask 方法
Laravel 8.69发布了 Str::mask()方法,该方法使用重复字符屏蔽字符串的一部分.
classPasswordResetLinkControllerextendsController{publicfunctionsendResetLinkResponse(Request $request) { $userEmail =User::where('email', $request->email)->value('email'); // username@domain.com $maskedEmail =Str::mask($userEmail,'*',4); // user***************// If needed, you provide a negative number as the third argument to the mask method,// which will instruct the method to begin masking at the given distance from the end of the string $maskedEmail =Str::mask($userEmail,'*',-16,6); // use******domain.com }}
// instead ofRoute::get('users/{user}/edit',function (User $user) {...})->middleware('can:edit,user');// you can do thisRoute::get('users/{user}/edit',function (User $user) {...})->can('edit''user');// PS: you must write UserPolicy to be able to do this in both cases
publicfunctiondownload(File $file){// Initiate file download by redirecting to a temporary s3 URL that expires in 5 secondsreturnredirect()->to(Storage::disk('s3')->temporaryUrl($file->name,now()->addSeconds(5)));}
if (request()->has('since')) {
// example.org/?since=
// fails with illegal operator and value combination
$query->whereDate('created_at', '<=', request('since'));
}
if (request()->input('name')) {
// example.org/?name=0
// fails to apply query filter because evaluates to false
$query->where('name', request('name'));
}
if (request()->filled('key')) {
// correct way to check if get parameter has value
}
class VerifyLicense
{
public function handle(Request $request, Closure $next, $type = null)
{
$licenseKey = match ($type) {
'basic' => $request->getPassword(),
'bearer' => $request->bearerToken(),
default => $request->get('key')
};
// Verify license and return response based on the authentication type
}
}
// List all the files from a folder
$files = Storage::disk('origin')->allFiles('/from-folder-name');
// Using normal get and put (the whole file string at once)
foreach($files as $file) {
Storage::disk('destination')->put(
"optional-folder-name" . basename($file),
Storage::disk('origin')->get($file)
);
}
// Best: using Streams to keep memory usage low (good for large files)
foreach ($files as $file) {
Storage::disk('destination')->writeStream(
"optional-folder-name" . basename($file),
Storage::disk('origin')->readStream($file)
);
}
// The has method returns true if the item is present & not null.
$request->session()->has('key');
// THe exists method returns true if the item ir present, event if its value is null
$request->session()->exists('key');
// THe missing method returns true if the item is not present or if the item is null
$request->session()->missing('key');
在处理高流量时,用 MySQL 统计像页面浏览量这样的东西可能会对性能造成相当大的影响。Redis 在这方面要好得多。你可以使用 Redis 和一个预定命令来保持与 MySQL 在一个固定的时间间隔内同步。
Route::get('{project:slug', function (Project $project) {
// Instead of $project->increment('views') we use Redis
// We group the views by the project id
Redis::hincrby('project-views', $project->id, 1);
})
// Console/Kernel.php
$schedule->command(UpdateProjectViews::class)->daily();
// Console/Commands/UpdateProjectViews.php
// Get all views from our Redis instance
$views = Redis::hgetall('project-views');
/*
[
(id) => (views)
1 => 213,
2 => 100,
3 => 341
]
*/
// Loop through all project views
foreach ($views as $projectId => $projectViews) {
// Increment the project views on our MySQL table
Project::find($projectId)->increment('views', $projectViews);
}
// Delete all the views from our Redis instance
Redis::del('project-views');
<?php
namespace App\Jobs;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class MyLongJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 3600;
private const CACHE_KEY = 'user.last-process-id';
public function handle()
{
$processedUserId = Cache::get(self::CACHE_KEY, 0); // Get last processed item id
$maxId = Users::max('id');
if ($processedUserId >= $maxId) {
Log::info("All users have already been processed!");
return;
}
while ($user = User::where('id', '>', $processedUserId)->first()) {
Log::info("Processing User ID: {$user->id}");
// Your long work here to process user
// Ex. Calling Billing API, Preparing Invoice for User etc.
$processedUserId = $user->id;
Cache::put(self::CACHE_KEY, $processedUserId, now()->addSeconds(3600)); // Updating last processed item id
if (app('queue.worker')->shouldQuit) {
$this->job->release(60); // Requeue the job with delay of 60 seconds
break;
}
}
Log::info("All users have processed successfully!");
}
}