1

私は2つのテーブルを1つ持っていてpropertyDetails、もう1つは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();
        });
    }

私がやりたいことはprojectsDetails、テーブル2から最初の関連画像をすべて選択することです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

4

3 に答える 3

2

エラーが示すように、コレクションで関係を取得しようとしています。関係はそのコレクション内のオブジェクト (レコード) に存在します。プロパティをループして、それぞれの項目を取得する必要があります...

 @foreach ($properties as $property)
  // access property properties here
  @foreach ($property->image as $image)
    // access image properties here.
  @endforeach
@endforeach
于 2016-12-14T10:28:18.817 に答える
0

この行を変更します。

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

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

そしてさらに試みる。

注: View::make に渡される 2 番目の引数は、ビューで使用できるようにする必要があるデータの配列です。

参照

于 2016-12-14T10:08:32.427 に答える
0

ここにいるすべての人に感謝します

@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
于 2016-12-14T11:15:48.267 に答える