我正在创建一个表来保存用户通过表单上传的多张照片。有人告诉我
you'd need to create a separate table for them (photos) create a separate model (Photo with a field 'src')
我的问题是 src。我是否需要将表的属性保存为 src
所以不是 $table->string('photo);
它的
$table->src('photo);
最佳答案
您将需要像这样定义迁移。
在您的照片表迁移中遵循以下步骤:
Schema::create('photos', function (Blueprint $table) {
$table->increments('id'); //you save this id in other tables
$table->string('title');
$table->string('src');
$table->string('mime_type')->nullable();
$table->string('title')->nullable();
$table->string('alt')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
仅供引用,照片的模型如下所示:
class Photo extends Model
{
protected $fillable = [
'title',
'src', //the path you uploaded the image
'mime_type'
'description',
'alt',
];
}
在其他表迁移中:
Schema::table('others', function(Blueprint $table){
$table->foreign('photo_id')->references('id')->on('photos');
});
其他与照片有关系的模特
class Other extends Model
{
public function photo()
{
return $this->belongsTo(Photo::class,'photo_id');
}
}
关于image - 在 Laravel 中存储照片。迁移文件的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48945935/