Php Laravel:联接两个表从表1获取信息,并从表2中获取第一个相关图像

Php Laravel:联接两个表从表1获取信息,并从表2中获取第一个相关图像,php,mysql,laravel,eloquent,laravel-eloquent,Php,Mysql,Laravel,Eloquent,Laravel Eloquent,我有两个表,一个是propertyDetails,另一个是propertyImages 我已经把这两张表联系起来了 这是模型 属性详细信息 class PropertyDetail extends \Eloquent { protected $fillable = []; public function propImages() { return $this->hasMany('PropertyImage', 'property_details_i

我有两个表,一个是
propertyDetails
,另一个是
propertyImages
我已经把这两张表联系起来了

这是模型

属性详细信息

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

    public function propImages()
    {
        return $this->hasMany('PropertyImage', 'property_details_id');
    }
}
class PropertyImage extends \Eloquent
{
    protected $fillable = [];

    public function propertyImages()
    {
        return $this->belongsTo('PropertyDetail', '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();
        });
    }
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();
        });
    }
属性图像

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

    public function propImages()
    {
        return $this->hasMany('PropertyImage', 'property_details_id');
    }
}
class PropertyImage extends \Eloquent
{
    protected $fillable = [];

    public function propertyImages()
    {
        return $this->belongsTo('PropertyDetail', '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();
        });
    }
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();
        });
    }
我要做的是从表2中选择带有第一个相关图像的所有
项目详细信息

我试过了

$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
得到

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

更改此行:

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

然后再试一次

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


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

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

谢谢你,这就是我如何让它工作的

在视图部分,正如@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

Mayank Pandeyz注意到更改了相同的错误,我只需使用(['ProPicages'=>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
很好,我的问题解决了,但最后一件事,我只需要得到第一张图片,这还不够。你可以将->get()更改为->first(),我不这么认为,因为我查询的是
属性,而不是
图像
问题是在第一个图像循环后如何刹车