laravel和laravel-admin使用记录
composer安装laravel5.5和laravel-admin
composer create-project --prefer-dist laravel/laravel blog "5.5.*" |
首先确保安装好了laravel,并且数据库连接设置正确。
composer require encore/laravel-admin
然后运行下面的命令来发布资源:
php artisan vendor:publish –provider=”Encore\Admin\AdminServiceProvider”
php artisan admin:install
报错:
D:\phpstudy_pro\WWW\company\blog>php artisan admin:install In Connection.php line 664: SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations) In PDOConnection.php line 31: SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES) In PDOConnection.php line 27: SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES) |
解决方案:修改database.php和.env.php文件的数据库内容。
D:\phpstudy_pro\WWW\company\blog>php artisan admin:install命令。 Migration table created successfully. In Connection.php line 664: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`)) In PDOStatement.php line 119: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes In PDOStatement.php line 117: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes |
删除users表,重新执行php artisan admin:install命令。
添加完成后如果http://company.t.com/admin/无法访问,则在public下的.htaccess添加如下内容
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L] |
去掉左侧菜单默认折叠
打开config/admin.php,修改layout,
去掉sidebar-collapse
设置语言
完成安装之后,默认语言为英文(en),如果要使用中文,打开config/app.php,将locale设置为zh-CN即可。
excel导出中文乱码问题
文件位置:vendor\encore\laravel-admin\src\Grid\Exporters\CsvExporter.php,添加 print(chr(0xEF).chr(0xBB).chr(0xBF));//设置utf-8 + bom
public function export()
{
$filename = $this->getTable().'.csv';
$headers = [
'Content-Encoding' => 'UTF-8',
'Content-Type' => 'text/csv;charset=UTF-8',
'Content-Disposition' => "attachment; filename=\"$filename\"",
];
print(chr(0xEF).chr(0xBB).chr(0xBF));//设置utf-8 + bom
...
} |
创建模型
php artisan make:model Config
php artisan make:model ./Models/GoodsCouponConfig // 模型路径 /app/Models/GoodsCouponConfig.php
模型中配置对应的表文件
protected $table = ‘config’;
创建控制器
php artisan admin:make RedPacketConfigController --model=App\\Models\\RedPacketConfig |
显示如下
D:\phpstudy_pro\WWW\company\blog>php artisan admin:make ConfigController --model=App\Models\Config
App\Admin\Controllers\ConfigController created successfully.
Add the following route to app/Admin/routes.php:
$router->resource('configs', ConfigController::class); |
在Admin/routes.php中添加$router->resource(‘configs’, ConfigController::class);即可访问。
添加API接口
routes/api.php中添加路由
Route::get('config/{config}', 'Api\ConfigController@test'); |
控制器app\Http\Controllers\Api\ConfigController.php中添加方法
public function test($config){
return $config;
} |
通过url域名/api/config/3访问即可。
图片上传配置
①admin.php文件配置如下
'upload' => [
// Disk in `config/filesystem.php`.
'disk' => 'admin',
// Image and file upload path under the disk above.
'directory' => [
'image' => 'images',
'file' => 'files',
],
], |
②filesystem.php文件添加如下配置
'disks' => [
......
'admin' => [
'driver' => 'local',
'root' => public_path('uploads'),
'visibility' => 'public',
'url' => env('APP_URL').'/uploads',
],
], |
③修改.env文件内容APP_URL
列显示
// 关联显示
$grid->column("productCategory.category_name",'分类');
// 自定义列显示
$grid->column("imgs",'产品缩略图')->display(function ($image) {
return "<img src='/uploads/{$image}' width='30px' height='30px'/>";
}); |
更多列示例demo参考
列展示示例
开启跨域
参考:https://learnku.com/articles/20051
模型追加属性
public $appends = [ 'sex_text', 'createtime_text', 'updatetime_text', 'canborn_text', ]; public function getCreatetimeTextAttribute(){ return date('Y-m-d H:i:s',$this->create_time); } public function getUpdatetimeTextAttribute(){ return date('Y-m-d H:i:s',$this->update_time); } public function getSexTextAttribute() { return $this->sex == 1 ? '公' : '母'; } public function getCanbornTextAttribute() { return $this->can_born == 1 ? '是' : '否'; } |
联动
protected function form() { $form = new Form(new Goods); $form->select("animal_type1_id", '总类型') ->options( AnimalType1::pluck('type_name', 'id') ) ->load('animal_type2_id', '/admin/goods_attrs'); $form->select("animal_type2_id", '二级类型') ->options(array(0 =>'请选择类型')); } public function attrs(Request $request){ $animalType1Id = $request->get('q'); return AnimalType3::where('type1_id', $animalType1Id)->pluck('type_name', 'id'); } |
Session store not set on request.
在/app/Http/Kernel.php文件里配置StartSession
protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \App\Http\Middleware\TrustProxies::class, \Illuminate\Session\Middleware\StartSession::class, ]; |