【发布时间】: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