有时,需要对分页代码进行自定义,那么此时就需要把分页视图发布出来:
php artisan vendor:publish --tag=laravel-pagination
有时,需要对分页代码进行自定义,那么此时就需要把分页视图发布出来:
php artisan vendor:publish --tag=laravel-pagination
不解释了,有需要的人会懂。
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class GuestbookCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return $this->collection->map(function ($item){
return [
'username' => $item->user->username,
'content' => $item->content,
'created_at' => $item->created_at->format('Y-m-d H:i:s'),
'reply_content' => $item->reply->content,
'id' => $item->id,
];
})->toArray();
}
}
在model中添加以下代码:
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format('Y-m-d H:i:s');
}
注意,需要引用:use DateTimeInterface;
这个表是laravel/sanctum这个包带的,Laravel Sanctum 是 Laravel 官方推出的一个轻量级的身份验证系统,专门用于为单页应用(SPA)、移动应用以及简单的 token-based API 提供身份验证解决方案,它提供了一种简洁且安全的方式来处理用户身份验证。
如果不需要,当然可以删除,执行下面的命令把它干掉:
composer remove laravel/sanctum
如果要支持多语言i18n,可以使用laravel-lang这个包,操作如下:
php artisan lang:publish
composer require laravel-lang/common
php artisan lang:update
php artisan lang:add zh_CN
翻译文件在lang文件夹下
如果要支持多语言切换,可以通过中间件实现:
php artisan make:middleware LanguageMiddleware
填写以下代码:
public function handle(Request $request, Closure $next): Response
{
if(session()->has('locale'))
app()->setLocale(session('locale'));
else
app()->setLocale(config('app.locale'));
return $next($request);
}
HomeController中
public function setLocale($locale)
{
session(['locale' => $locale]);
return redirect()->back();
}
路由:
Route::get('/setLocale/{locale}', [HomeController::class, 'setLocale'])->name('home.setLocale');
视图:
<ul class="menu">
<li><a href="{{ route('home.setLocale', ['locale'=>'zh_CN']) }}"><h3>简体中文</h3></a></li>
<li><a href="{{ route('home.setLocale', ['locale'=>'en']) }}"><h3>English</h3></a></li>
</ul>
文本的翻译用法:
{{ __('iretailer.sign_out') }}
这句话的意思是在iretailer.php文件中找sign_out这个配置。当然,不要忘了在不同的语言文件夹中iretailer.php这个文件。
编辑nginx的网站配置文件,在location / {} 里面添加如下代码:
try_files $uri $uri/ /index.php?$query_string;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
有时,需要判断一个请中的某个字段是否为空,可以使用filled函数进行,代码如下:
if ($request->filled('keyword')) {
dd('keyword is not empty.');
}else {
dd('keyword is empty.');
}
作为一个全栈开发框架,Laravel的模型默认启用created_at和update_at字段的,有些情况下,我们并不想启用这两个字段,例如使用已经设计好的数据库,此时,只需要在模型中添加如下属性即可:
public $timestamps = false;
嗯,超级简单,代码还没有描述文字多🤔。
开发一个Web系统,上传文件是避免不了的,这在Laravel中实现非常简单,代码如下:
public function upload(Request $request)
{
if ($request->hasFile('file')) {
$file = $request->file('file');
$fileName = Carbon::now()->timestamp . '.' . $file->getClientOriginalExtension();
$destinationPath = 'uploads';
$file->move($destinationPath, $fileName);
return response()->json(['code'=>0, 'msg'=>'上传成功!', 'data'=>[
'guarantee_file' => $fileName,
'originalName' => $file->getClientOriginalName(),
]]);
}
return response()->json(['code'=>-1, 'msg'=>'请选择文件']);
}
表单不要忘记添加: enctype="multipart/form-data"
在Laravel中如果需要验证码,可以使用mews/captcha库来实现,过程如下 :
安装:
composer require "mews/captcha"
发布配置文件:
php artisan vendor:publish
前端引用:
<img src="{{captcha_src()}}" style="cursor: pointer" onclick="this.src='{{captcha_src()}}'+Math.random()">
注意,项目路径不能有中文,否则会报错:ErrorException: imagettfbbox(): Could not find/open font
简单吧,这就是生态强大的好处。