【问题标题】:Storing a photo in Laravel. Migration file's structure在 Laravel 中存储照片。迁移文件的结构
【发布时间】:2018-08-03 09:31:16
【问题描述】:

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

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

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

它的

$table->src('photo);

【问题讨论】:

    标签: image laravel migration database-migration


    【解决方案1】:

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

    在您的照片表迁移中遵循以下内容:

     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 吗? (与照片有关系的那个)
    • 。我应该在我的控制器中做什么。我已经通过表单保存了广告,那么我应该如何将 photo_id 添加到主表中。迁移 photo_id 在表中不存在时出现错误
    猜你喜欢
    • 2017-11-12
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2021-04-21
    • 2018-03-09
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    相关资源
    最近更新 更多