1

我有两张表,一张是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();
        });
    }

属性图像

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

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

我试过

$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

4

3 回答 3

2

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

 @foreach ($properties as $property)
  // access property properties here
  @foreach ($property->image as $image)
    // access image properties here.
  @endforeach
@endforeach
于 2016-12-14T10:28:18.817 回答
0

更改此行:

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

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

然后再试一次。

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

参考

于 2016-12-14T10:08:32.427 回答
0

感谢这里的一切是我如何让它工作

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

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

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

我只是改变了第二个@foreach使用slice 这里是最终代码

@foreach ($properties as $property)
      // access property properties here
      @foreach($property->propImages->slice(0, 1) as $image)
        // access image properties here.
      @endforeach
    @endforeach
于 2016-12-14T11:15:48.267 回答