从数据库中读写更改普通的数据没问题了,那么blob字段呢?
我扩充了全面的jyxx表,在后面增加了两个blob字段
jyzstp,一个想显示图片,
jyfjwj,一个想添加任意附件。
JyxxController.php的修改
在detail函数中增加
$show->jyzstp(‘交易展示图片’)->image();
$show->jyfjwj(‘交易附加文件’)->file();
在form函数中增加
$form->image(‘jyzstp’, ‘交易展示图片’);
$form->file(‘jyfjwj’, ‘交易附加文件’);
后面会附上完整的 JyxxController.php
问题来了,点击显示时,怎么不显示图片出来?
结果新增没问题,一到显示时就不对。看着类似的文章就开始改,最后发现就是因为不明所以的时候,就对\config\filesystems.php做了不恰当的更改。然后就是到处查找问题。
经常在网上查信息,一个好处就是全,什么信息都有;
但一个缺点就是杂,首先良莠不齐,其次都是只言片语,最后每个人的环境不同说法就一定不一致。
改的不对,接着就是一顿查找,目录问题?权限问题?软连接问题?还好最后梳理清楚了。
创建目录的软连接
我的项目目录是D:\mydeb\Laravel\blog
在这下面执行
>php artisan storage:link
The [D:\mydev\Laravel\blog\public\storage] link has been connected to [D:\mydev\Laravel\blog\storage\app/public].
The links have been created.
为何要创建软连接
反思一下,apache也好、Nginx也好,用wamp、phpstudy都一样,它们都会设置一个叫网页根目录的一个参数,至于叫做root、DocumentHome、DocumentRoot什么的都无所谓,总之web server程序会以这个目录为起点,把它展示在http://…/这个后面。虚拟目录也都类似,就是要做映射。这样当http请求到来是,web server就知道去哪里找文件,然后才会涉及到文件的解析等问题。
那么laraval呢?
我是在项目目录下(D:\mydeb\Laravel\blog)运行的
>php artisan serve
它会把当前目录下的app\Admin,自动映射为http://…/Admin,也就是提示我们访问的
http://localhost:8000/admin
图片访问也要有一个映射,告诉我们去哪里找图片
再看前面的【创建目录的软连接】
php artisan storage:link
这个命令是创建目录的软连接?什么依据呢?
就是D:\mydev\Laravel\blog\config\filesystems.php这个文件,要查看这个文件中的信息,然后创建软连接,下面是filesystems.php文件中的后面部分
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
如果英语不好,就百度翻译一下:
在这里,可以配置在执行storage:link
Artisan命令时将创建的符号链接。数组键应该是链接的位置,值应该是它们的目标。
所以php artisan storage:link运行时才会有如下输出,
The [D:\mydev\Laravel\blog\public\storage] link has been connected to [D:\mydev\Laravel\blog\storage\app/public].
就是已经把D:\mydev\Laravel\blog\public\storage目录,映射到了D:\mydev\Laravel\blog\storage\app/public目录下。
为何要映射呢?因为D:\mydev\Laravel\blog\storage\app/public目录下是url可访问的公共目录,而其他目录不是,上传文件的storage目录,只是在上传时后台保存文件使用,不能实现对外访问。
软连接创建成功后,这里将有一个快捷键的图标,目录的内容会同步,但不是复制,就是linux下的ln -s的作用。
配置admin用户的文件目录映射关系
'admin' => [
'driver' => 'local',
// 'root' => storage_path('public/upload'),
// 'url' => env('APP_URL').'/storage/upload',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
我把admin用户的root和url注释掉,直接使用public组的映射关系
这样图片和文档就能正常显示、下载了
查看一下当前页面的源码,图片应该是
<img src='http://localhost:8000/storage/images/g005.png' style='max-width:200px;max-height:200px' class='img' />
附件应该是
<a href="http://localhost:8000/storage/files/程序功能说明书.docx" class="btn btn-default btn-xs pull-right" target="_blank" download='程序功能说明书.docx'>
附-完整的filesystems.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'admin' => [
'driver' => 'local',
// 'root' => storage_path('public/upload'),
// 'url' => env('APP_URL').'/storage/upload',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
附-完整的JyxxController.php
<?php
namespace App\Admin\Controllers;
use App\Models\Jyxx;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class JyxxController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = '交易信息库';
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new Jyxx());
$grid->jyxh('交易序号')->sortable();
$grid->jyrq('交易日期')->sortable();
$grid->jyjf('交易甲方');
$grid->jyyf('交易乙方');
$grid->jyzt('交易主题');
return $grid;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(Jyxx::findOrFail($id));
$show->jyxh('交易序号');
$show->jyrq('交易日期');
$show->jyjf('交易甲方');
$show->jyyf('交易乙方');
$show->jyzt('交易主题');
$show->jyje('交易金额');
$show->jyxs('交易形式');
$show->jynr('交易内容');
$show->jyjj('交易简介');
$show->jyzstp('交易展示图片')->image();
$show->jyfjwj('交易附加文件')->file();
return $show;
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new Jyxx());
$form->number('jyxh', '交易序号');
$form->datetime('jyrq', '交易日期');
$form->text('jyjf','交易甲方');
$form->text('jyyf','交易乙方');
$form->text('jyzt','交易主题');
$form->decimal('jyje','交易金额');
$form->radio('jyxs', __('交易形式'))
->options(['网签' => __('网签'), '纸质' => __('纸质')]);
$form->text('jynr','交易内容');
$form->textarea('jyjj','交易简介');
$form->image('jyzstp', '交易展示图片');
$form->file('jyfjwj', '交易附加文件');
return $form;
}
}
这就是折腾一天的情况,没人教,真慢啊。
继续学