【问题标题】:Laravel : Join two tables get the info from table one and get the first related image from table twoLaravel:连接两个表从表一中获取信息并从表二中获取第一个相关图像
【发布时间】:2017-04-29 14:48:06
【问题描述】:

我有两张桌子,一张是propertyDetails,另一张是propertyImages 我已经像这样完成了两个表之间的关系..

这是模型

物业详情

class PropertyDetail extends \Eloquent {
    protected $fillable = [];

    public function propImages()
    {
        return $this->hasMany('PropertyImage', 'property_details_id');
    }
}

表迁移

public function up()
    {
        Schema::create('property_details', function (Blueprint $table) {
            $table->increments('id');
            $table->enum('purpose', array('Sell', 'Rent'));
            $table->integer('property_owner_id')->unsigned();
            $table->integer('property_agent_id')->nullable();
            $table->integer('bedroom');
            $table->integer('dining_room');
            $table->integer('bathroom');
            $table->string('title', 100);
            $table->integer('price');
            $table->string('type', 120);
            $table->string('specify_type', 120);
            $table->text('details');
            $table->integer('active');
            $table->foreign('property_owner_id')->references('id')->on('property_owners')->onDelete('cascade');
            $table->timestamps();
        });
    }

propertyImages

class PropertyImage extends \Eloquent
{
    protected $fillable = [];

    public function propertyImages()
    {
        return $this->belongsTo('PropertyDetail', 'property_details_id');
    }
}

表迁移

Schema::create('property_images', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('property_details_id')->unsigned();
            $table->foreign('property_details_id')->references('id')->on('property_details')->onDelete('cascade');
            $table->binary('image');
            $table->timestamps();
        });
    }

我想做的是从表二propertyImages中选择所有projectsDetails和第一个相关图像

我试过了

$properties = PropertyDetail::with('propImages')->get();
return View::make('admin.properties.view', compact('properties'));

在我看来

@foreach($properties as $property)
{{ HTML::image('images/propertyImages/'.$property->propImages->image, $property->title, array('width'=>767, 'height'=>384)) }}
@endforeach

得到

未定义属性:Illuminate\Database\Eloquent\Collection::$image

【问题讨论】:

    标签: php mysql laravel eloquent laravel-eloquent


    【解决方案1】:

    正如错误所说,您正在尝试获取集合上的关系,该关系存在于该集合中的对象(记录)上。您需要遍历属性,并为每个属性获取它们的项目...

     @foreach ($properties as $property)
      // access property properties here
      @foreach ($property->image as $image)
        // access image properties here.
      @endforeach
    @endforeach
    

    【讨论】:

    • 很高兴你把我的问题说对了,但最后一件事我只需要获取第一张图片,而不是全部
    • 你可以把你的 ->get() 改成 ->first()
    • 我不这么认为,因为我查询的是 properties 而不是 images 问题是如何在第一个图像循环后刹车
    【解决方案2】:

    改变这一行:

    return View::make('admin.properties.view', compact('properties'));
    

    return View::make('admin.properties.view', ['properties' => $properties]));
    

    然后再试一次。

    注意:传递给 View::make 的第二个参数是一个数据数组,应该提供给视图。

    Reference

    【讨论】:

    • Mayank Pandeyz 同样的事情注意到改变了同样的错误,我想试试这个$properties = PropertyDetail::with(['propImages' => function($query){ $query->where('property_details_id', 'id'); }])->get();,结果还是一样
    • 关于注意事项:我的观点看起来像@foreach($properties as $property) {{ HTML::image('images/propertyImages/'.$property->propImages->image, $property->title, array('width'=>767, 'height'=>384)) }} @endforeach
    【解决方案3】:

    谢谢大家,这就是我如何让它工作

    在@Ferran 建议的视图部分中,我这样做了

    @foreach ($properties as $property)
      // access property properties here
      @foreach ($property->image as $image)
        // access image properties here.
      @endforeach
    @endforeach
    

    但是因为我只需要第一张图片而不是全部我找到了解决方案here

    我刚刚将第二个@foreach 改为使用slice 这是最终代码

    @foreach ($properties as $property)
          // access property properties here
          @foreach($property->propImages->slice(0, 1) as $image)
            // access image properties here.
          @endforeach
        @endforeach
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 2021-04-28
      • 2019-07-17
      • 2016-01-31
      相关资源
      最近更新 更多