Image 在Laravel中存储照片。迁移文件';s结构
我正在创建一个表来保存用户通过表单上传的多张照片。我被告知 您需要为它们创建一个单独的表(照片)创建一个单独的模型(带有字段“src”的照片) 我的问题是src。是否需要将表的属性另存为src 因此,不要使用Image 在Laravel中存储照片。迁移文件';s结构,image,laravel,migration,database-migration,Image,Laravel,Migration,Database Migration,我正在创建一个表来保存用户通过表单上传的多张照片。我被告知 您需要为它们创建一个单独的表(照片)创建一个单独的模型(带有字段“src”的照片) 我的问题是src。是否需要将表的属性另存为src 因此,不要使用$table->string('photo') 其 您需要像这样定义迁移 在照片表中,请执行以下操作: Schema::create('photos', function (Blueprint $table) { $table->increments('id'); /
$table->string('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');
}
}
我需要广告表中的src吗。(与照片有关系的照片)。我应该在控制器中执行什么操作。我已经通过表单保存了一则广告,因此我应该如何将照片id添加到主表中。当表中不存在迁移照片id时,我收到错误
class Other extends Model
{
public function photo()
{
return $this->belongsTo(Photo::class,'photo_id');
}
}