管理文件附件
建议编辑本指南是教程“数据管理”的延续,我们将在其中分析“附件文件”的工作。
让我们回到之前创建的Post
模型,并添加一个新的列hero
,在其中我们将存储博客文章的主图信息:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* 运行迁移。
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->string('hero')->nullable();
});
}
/**
* 回滚迁移。
*
* @return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('hero');
});
}
}
添加到模型以自动记录:
// app/Models/Post.php
protected $fillable = [
'title',
'description',
'body',
'author',
'hero'
];
//..
要将数据写入新列,需要在我们的创建/编辑屏幕中添加合适的字段,使用Cropper
来裁剪图像:
// app/Orchid/Screens/PostEditScreen.php
public function layout(): array
{
return [
Layout::rows([
Input::make('post.title')
->title('标题')
->placeholder('吸引但神秘的标题'),
Cropper::make('post.hero')
->title('大型网页横幅图像,通常在前面和中心位置')
->width(1000)
->height(500),
TextArea::make('post.description')
->title('描述')
->rows(3)
->maxlength(200)
->placeholder('预览的简短描述'),
Relation::make('post.author')
->title('作者')
->fromModel(User::class, 'name'),
Quill::make('post.body')
->title('主要文本'),
])
];
}
注意。 如果在文件上传过程中,您没有在最终结果中看到您的图像,请检查
upload_max_filesize
和post_max_size
设置,您可能需要增加这些值。
对于这样的字段,额外指明了宽度和高度,以确保比例显示给用户。保存带有图像的记录后,完整的url
地址将被写入数据库列,例如:
http://localhost:8000/storage/2019/08/02/0f92ef693c26f3c1dbe2e3792abac9254ee98310.png
注意。 图像链接是从您的配置文件中指定的
url
地址生成的,对于通过内置Web服务器进行本地开发,需要指定端口。
这是最简单的输入,但如果您决定使用https
或更改域名怎么办?为此,最好使用相对表示法:
Cropper::make('post.hero')
->targetRelativeUrl(),
另一种写入选项是使用关系,为此Cropper
字段将记录下载文件的编号:
Cropper::make('post.hero')
->targetId(),
注意。 为了使您的数据库保持一致,请使用建议的选项之一。
让我们专注于记录编号,但首先删除所有现有记录。
通常,一些文件会被组合在一起,例如,显示在整个组中,Upload
字段适合这种情况。
public function layout(): array
{
return [
Layout::rows([
Input::make('post.title')
->title('标题')
->placeholder('吸引但神秘的标题'),
Cropper::make('post.hero')
->targetId()
->title('大型网页横幅图像,通常在前面和中心位置')
->width(1000)
->height(500),
TextArea::make('post.description')
->title('描述')
->rows(3)
->maxlength(200)
->placeholder('预览的简短描述'),
Relation::make('post.author')
->title('作者')
->fromModel(User::class, 'name'),
Quill::make('post.body')
->title('主要文本'),
Upload::make('post.attachments')
->title('所有文件')
])
];
}
该字段将与之前的字段不同,因为数据不会明确应用于Post
模型,而是通过连接加载和保存,为此我们必须指明Attachable
特性:
use Illuminate\Database\Eloquent\Model;
use Orchid\Attachment\Attachable;
use Orchid\Attachment\Models\Attachment;
use Orchid\Screen\AsSource;
class Post extends Model
{
use AsSource, Attachable;
//...
}
还要通过关系在我们的屏幕中描述依赖记录的同步:
public function createOrUpdate(Request $request)
{
$this->post->fill($request->get('post'))->save();
$this->post->attachments()->syncWithoutDetaching(
$request->input('post.attachments', [])
);
Alert::info('您已成功创建了一篇文章。');
return redirect()->route('platform.post.list');
}
保存后,连接将与我们的记录在表attachmentable
中建立:
id attachmentable_type attachmentable_id attachment_id
1 App\Post 3 101
2 App\Post 3 102
3 App\Post 3 103
4 App\Post 3 104
但当再次访问记录时,字段将为空。这是因为query
不知道我们模型的附加记录。我们将通过添加下载来解决这个问题:
public function query(Post $post): array
{
$post->load('attachments');
return [
'post' => $post
];
}