gpt4 book ai didi

php - 拉维尔 : Join two tables get the info from table one and get the first related image from table two

转载 作者:行者123 更新时间:2023-11-29 01:51:28 25 4
gpt4 key购买 nike

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

我想做的是从表二 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

得到

Undefined property: Illuminate\Database\Eloquent\Collection::$image

最佳答案

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

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

关于php - 拉维尔 : Join two tables get the info from table one and get the first related image from table two,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41139780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com