在 Laravel 中存储照片。迁移文件结构,

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48945935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:26:10  来源:igfitidea点击:

Storing a photo in Laravel. Migration file structure,

imagelaravelmigrationdatabase-migration

提问by Andre

I'm creating a table to hold multiple photos a user uploads through a form. I was told to

我正在创建一个表格来保存用户通过表单上传的多张照片。我被告知

you'd need to create a separate table for them (photos) create a separate model (Photo with a field 'src')

您需要为他们创建一个单独的表(照片)创建一个单独的模型(带有字段“src”的照片)

My issue is with the src. Do i need to save a property of the table as a src so instead of $table->string('photo);

我的问题是 src。我是否需要将表的属性保存为 src 而不是$table->string('photo);

its

它的

$table->src('photo);

回答by Mahdi Younesi

you will need to define migrations like these.

您将需要像这样定义迁移。

In your photo table migration follow this:

在您的照片表迁移中,请遵循以下步骤:

 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();
    });

FYI the photo's model would look like this:

仅供参考,照片的模型如下所示:

class Photo extends Model
{
   protected $fillable = [
    'title',
    'src', //the path you uploaded the image
    'mime_type'
    'description',
    'alt',
  ];
}

In Other table migration:

在其他表迁移中:

 Schema::table('others', function(Blueprint $table){
        $table->foreign('photo_id')->references('id')->on('photos');
 });

Othermodel which has relationship with photo

与照片有关系的其他型号

class Other extends Model
{

 public function photo()
 {
     return $this->belongsTo(Photo::class,'photo_id');
 }

}