Laravel:加入两个表从表1中获取信息并从表2中获取第一个相关图像

时间:2016-12-14 10:05:01

标签: php mysql laravel eloquent laravel-eloquent

我有两个表格propertyDetails,另一个是propertyImages 我已经完成了两个表之间的关系..

这是模型

propertyDetails

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

我要做的是选择所有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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

感谢所有人,我是如何让它工作的

在视图部分中@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

答案 2 :(得分:0)

更改此行:

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

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

再试一次。

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

Reference