image - 在 Laravel 中存储照片。迁移文件的结构

标签 image laravel migration database-migration

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

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/

相关文章:

jquery - 使用 Wordpress 和 jQuery 替换图像

mysql - Laravel orderBy 大大减慢了响应速度

php - 在 laravel 的特定时间显示通知消息

image - 使用 Yii2 在数据库中上传多个文件 URL

css - 使用CSS使两个透明图像重叠

php - 如何在不影响较小图像的情况下使网页上的图像适合最大宽度?

php - Laravel数据库插入错误

node.js - 使用迁移 API 时 Knex 迁移不起作用

wpf - 使用迁移到 LocalDB 的 CodeFirst,我可以指定*在哪里*创建数据库吗?

ruby-on-rails - 添加索引 :unique to a column in ruby on rails via generate migration