2016-12-14 102 views
2

第一相关图像我有两个表一个是propertyDetails,另一个是propertyImages ,我已经做了,像这样的两个表之间的关系..Laravel:加入两个表得到一个表的信息,并从表中的两

这里是车型

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 

未定义的属性:照亮\数据库\雄辩\收藏:: $图像

回答

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 
0

改变这一行:

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

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

,然后再试一次。

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

Reference

+0

Mayank Pandeyz同样的事情提的改变同样的错误,我只是事件试试这个'$性能= PropertyDetail ::用([ 'propImages'=>函数($查询){$ 查询 - > where('property_details_id','id');我的视图看起来就像'@foreach($ properties as $ property) {{HTML :: image('images/propertyImages /');}获得();' 并且仍然是相同的结果 –

+0

'。$ property-> propImages-> image,$ property-> title,array('width'=> 767,'height'=> 384))}} @ endforeach' –

2

由于错误说,你要得到一个集合的关系,这种关系存在于该集合中的对象(记录)。你需要遍历属性,并为每一个获得他们的项目...

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

不错,你有正确的问题,但只是有一件事是最后我需要得到的第一个图像不只是所有 –

+0

你可以改变你的 - > get()到 - > first() – Ferran

+0

我不这么认为,因为我查询的是'属性'而不是'images'问题是如何在第一次图像循环后制动 –