阅读目录
- Laravel 常用命令
- Laravel 中 查询构造器 where 中拼接in查询方法
- get_class 获取实例的类名
- DB 应用技巧
- 获取请求地址
- 如果你想要在运行时开启/关闭 Debugbar,可以通过如下方式:
- laravel将数组转换成集合
- laravel的resource控制器
- laravel中的路由控制器执行command中的命令
- 命令
- laravel集合转数组
- laravel获取当前的url以及当前的基础域名方法汇总
- laravel的validation中文文件
- laravel中使一段文字,限制长度,并且超出部分使用指定内容代替
- laravel本地开发的项目部署到服务器上
- Laravel 更新数据时在表单请求验证中排除自己,检查指定字段唯一性
- laravel文件上传(本人使用的ftp驱动配置,本地测试总结)
- Model中的设置
- Laravel 清除缓存创建模型技巧
Laravel 常用命令
Laravel查看版本
php artisan --version
Laravel Framework 5.8.38
项目安装
composer require --dev phpunit/phpunit
全局安装
composer global require --dev phpunit/phpunit
安装laravel
composer create-project laravel/laravel=5.8.* laravel5 --prefer-dist
安装 Debugbar
composer require --dev barryvdh/laravel-debugbar
清除缓存
php artisan route:clear
php artisan config:clear
php artisan cache:clear
新建 Model
php artisan make:model Dishes
生成目录:D:\Project\app\Dishes.php
Laravel 中 查询构造器 where 中拼接in查询方法
<?php
$field = 'id';
$ids= ['1,2,3,4,6'];
$whereIn = function ($query) use( $field , $ids ){
$query->whereIn( $field, $ids );
};
$menu=Permission::where($whereIn)
->where(array('pid'=>'-1','is_menu'=>'0'))
->orderBy('sort','asc')
->select('name as title','icon','url as href','pid','id','is_menu')
->get()
->toArray();
get_class 获取实例的类名
dd(get_class(request()));
DB 应用技巧
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class TxtController extends Controller
{
public function createOrder()
{
$first = self::getDb()->where("id", 1)->first();
dd($first);
}
public static function getDb()
{
return DB::table(self::tableName());
}
public static function tableName()
{
return 'users';
}
}
获取请求地址
dd(request()->path());
如果你想要在运行时开启/关闭 Debugbar,可以通过如下方式:
\Debugbar::enable();
\Debugbar::disable();
laravel将数组转换成集合
$cars = [
"Volvo" => "asdfVolvo",
"BMW" => "asdfBMW",
"Toyota" => "asdfToyota",
];
$myArray = collect($cars);
var_dump($myArray);
laravel的resource控制器
php artisan make:controller UserController --rescource
方法名称 | 作用 | URL |
---|---|---|
index | 显示登入页面 | user/ |
create | 显示新建用户的表单 | user/create |
store | 把新建的用户存储到数据库中 | user/ |
edit | 显示修改某个用户的表单 | user/{id}/edit |
update | 将修改后存入到数据库 | user/{id} |
destroy | 删除某个用户 | user/{id} |
laravel中的路由控制器执行command中的命令
Route::get('/foo', function () {
$exitCode = Artisan::call('command_name');
//
});
命令
–plain 生成一个空白的控制器类:php artisan make:controller MailController --plain
清空配置缓存文件:php artisan config:clear
查看端口:netstat -an | grep 80
动态查看日志文件:tail -f access.log
模型缓存命令:php think optimize:schema
路由缓存命令:php think optimize:route
laravel集合转数组
# 转换成数组
$user = User::with('roles')->first();
return $user->toArray();
注意:也可以把整个的模型集合转换成数组:
return User::all()->toArray();
要把模型转换成 JSON,可以使用 toJson 方法:
return User::find(1)->toJson();
laravel获取当前的url以及当前的基础域名方法汇总
// 返回当前页面的地址:http://a.com/platforms
URL::full();
url()->full();
// 返回当前页面的完整路径:http://a.com/platforms
URL::current();
url()->current();
// 返回前一个页面的地址:http://a.com
URL::previous();
url()->previous();
// https://jiahe.com/css/foo.css
URL::secureAsset('css/foo.css');
// 返回当前页面的完整路径url: http://xx.com/aa/bb
Request::url();
$request->url()
// 路径: /aa/bb
Request::path();
$request->path();
// 获取请求 Uri: /aa/bb/?c=d
Request::getRequestUri();
$request->getRequestUri();
// 获取 Uri: http://xx.com/aa/bb/?c=d
Request::getUri();
$request->getUri();
laravel的validation中文文件
使用方法:
直接替换resources/lang/en/validation.php中的内容
<?php
return [
'unique' => ':attribute 已存在',
'accepted' => ':attribute 是被接受的',
'active_url' => ':attribute 必须是一个合法的 URL',
'after' => ':attribute 必须是 :date 之后的一个日期',
'alpha' => ':attribute 必须全部由字母字符构成。',
'alpha_dash' => ':attribute 必须全部由字母、数字、中划线或下划线字符构成',
'alpha_num' => ':attribute 必须全部由字母和数字构成',
'array' => ':attribute 必须是个数组',
'before' => ':attribute 必须是 :date 之前的一个日期',
'between' => [
'numeric' => ':attribute 必须在 :min 到 :max 之间',
'file' => ':attribute 必须在 :min 到 :max KB之间',
'string' => ':attribute 必须在 :min 到 :max 个字符之间',
'array' => ':attribute 必须在 :min 到 :max 项之间',
],
'boolean' => ':attribute 字符必须是 true 或 false',
'confirmed' => ':attribute 二次确认不匹配',
'date' => ':attribute 必须是一个合法的日期',
'date_format' => ':attribute 与给定的格式 :format 不符合',
'different' => ':attribute 必须不同于:other',
'digits' => ':attribute 必须是 :digits 位',
'digits_between' => ':attribute 必须在 :min and :max 位之间',
'email' => ':attribute 必须是一个合法的电子邮件地址。',
'filled' => ':attribute 的字段是必填的',
'exists' => '选定的 :attribute 是无效的',
'image' => ':attribute 必须是一个图片 (jpeg, png, bmp 或者 gif)',
'in' => '选定的 :attribute 是无效的',
'integer' => ':attribute 必须是个整数',
'ip' => ':attribute 必须是一个合法的 IP 地址。',
'max' => [
'numeric' => ':attribute 的最大长度为 :max 位',
'file' => ':attribute 的最大为 :max',
'string' => ':attribute 的最大长度为 :max 字符',
'array' => ':attribute 的最大个数为 :max 个',
],
'mimes' => ':attribute 的文件类型必须是:values',
'min' => [
'numeric' => ':attribute 的最小长度为 :min 位',
'string' => ':attribute 的最小长度为 :min 字符',
'file' => ':attribute 大小至少为:min KB',
'array' => ':attribute 至少有 :min 项',
],
'not_in' => '选定的 :attribute 是无效的',
'numeric' => ':attribute 必须是数字',
'regex' => ':attribute 格式是无效的',
'required' => ':attribute 字段必须填写',
'required_if' => ':attribute 字段是必须的当 :other 是 :value',
'required_with' => ':attribute 字段是必须的当 :values 是存在的',
'required_with_all' => ':attribute 字段是必须的当 :values 是存在的',
'required_without' => ':attribute 字段是必须的当 :values 是不存在的',
'required_without_all' => ':attribute 字段是必须的当 没有一个 :values 是存在的',
'same' => ':attribute 和 :other 必须匹配',
'size' => [
'numeric' => ':attribute 必须是 :size 位',
'file' => ':attribute 必须是 :size KB',
'string' => ':attribute 必须是 :size 个字符',
'array' => ':attribute 必须包括 :size 项',
],
'url' => ':attribute 无效的格式',
'timezone' => ':attribute 必须个有效的时区',
/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/
'attributes' => [
'username' => '用户名',
'account' => '账号',
'captcha' => '验证码',
'mobile' => '手机号',
'password' => '密码',
'content' => '内容',
'identity' => '手机号/用户名',
'title' => '标题',
],
];
laravel中使一段文字,限制长度,并且超出部分使用指定内容代替
{{str_limit($post->content,100,'....')}}
laravel本地开发的项目部署到服务器上
不建议用ftp,建议用git、svn等工具从你的开发服务器上拉取。或者使用一些自动构建发布的工具。如果非要用ftp上传,建议用以下步骤:
1、服务器上安装composer,并使用composer self-update
更新到最新版
2、用ftp上传文件,保持目录结构。注意不要上传vendor文件夹
3、到项目目录下,使用命令composer install --no-dev
安装依赖
4、使用composer dump-autoload -o
优化自动加载
Laravel 更新数据时在表单请求验证中排除自己,检查指定字段唯一性
需求场景
修改用户信息时,在表单请求验证中排除当前邮箱所在的记录行,并检查邮箱的唯一性。
Laravel版本
5.2
路由
backend/user/{user}
实例
<?php
namespace App\Http\Requests\Backend\User;
use App\Http\Requests\Request;
class UpdateRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$id = $this->route('user'); //获取当前需要排除的id,这里的 user 是 路由 {} 中的参数
return [
'email' => "required|email|unique:users,email,".$id,
];
}
}
验证说明
unique:表名,字段,需要排除的ID
laravel文件上传(本人使用的ftp驱动配置,本地测试总结)
1.电脑端在:控制面板->程序和功能->打开和关闭Window功能,安装:Internet信息服务的(Ftp服务器,web管理工具的IIS管理服务,万网服务的常见http功能)
2.在电脑左下角搜索IIS服务,自行网上搜索ftp站点本地配置方法和Hosts文件中配置本地临时域名
效果绑定之后如下:
ftp://IP地址 看是否可以正常访问你所绑定的文件夹目录。 laraveltest.com:801/绑定目录下文件名。看是否能正常打开你的文件夹下的目录)
3.在框架config->filesystems.php中配置ftp站点:新增ftp驱动配置就可以了
'disks'=>[
'ftp' => [
'driver' => 'ftp',
'host' => '192.168.16.105', //本地电脑绑定的IP地址:
'username' => 'miniapp',//ftp账号:
'password' => '123456'//ftp密码:
],
]
4.把在ftp站定绑定的本地设置的域名放到config中的app.php配置文件中,方便调用,也方便以后统一修改。
‘imgurl’=>url(’/’).’:801/’
5.上传文件:
路由:
Route::any(‘demos’,[‘uses’=>‘StudentController@demos’]);
控制器:
public function demos(Request $request)
{
if (Request::isMethod("post")) {
//Storage::disk('ftp')->put('file.txt','Content');
$file = Request::file('uploads');
//dd($file);
//文件是否上传成功:
//扩展名:
$ext = $file->getClientOriginalExtension();
//临时绝对路径:
$realPath = $file->getRealPath();
$filename = date('YmdHis') . uniqid() . '.' . $ext;
$bool = Storage::disk('ftp')->put($filename, file_get_contents($realPath));
$img="<img src='".config('app.imgurl').$filename."'/>";
return $img;
} else {
return view('demos');
}
}
上传成功之后,就会显示上传的图片了。本人本地wampser安装到d盘,ftp站点目录设置到e盘了。
Model中的设置
Laravel指定表名
protected $table = 'customer';
`$primaryKey`属性(可选),值是主键名称,在主键不是id的时候则需要指定主键
protected $primaryKey = "uid";
下面即是允许入库的字段,数组形式字段允许入库
protected $fillable = ["parent_id","order","title","icon","uri"];
默认操作表中的`create_at`和`updated_at`字段,`false`,表示不要操作这两个字段
public $timestamps = false;
设置不允许入库字段,一般和$fillable存在一个即可
protected $guarded = [];
Laravel 清除缓存创建模型技巧
Route::get('/clear-cache', function() {
\Artisan::call('cache:clear');
return "Cache is cleared";
});
Route::get('/artisan/{name}',function($name){
\Artisan::call('make:model '.$name);
return $name;
});