简体   繁体   中英

Laravel : Join two tables get the info from table one and get the first related image from table two

I have two table one is propertyDetails and the other is propertyImages and I have done the relation between the two tables like so..

here is the models

propertyDetails

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

    public function propImages()
    {
        return $this->hasMany('PropertyImage', 'property_details_id');
    }
}

Table migration

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');
    }
}

Table migration

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();
        });
    }

what I want to do is select all the projectsDetails with the first related image from table two propertyImages

I tried

$properties = PropertyDetail::with('propImages')->get();
return View::make('admin.properties.view', compact('properties'));

and in my view

@foreach($properties as $property)
{{ HTML::image('images/propertyImages/'.$property->propImages->image, $property->title, array('width'=>767, 'height'=>384)) }}
@endforeach

got

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

As the error says, you're trying to get a relationship on a collection, the relationship exists on the objects (records) in that collection. You need to loop over the Properties, and for each one get their items...

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

Thanks for all here is how I got it to work

in the view section as @Ferran suggested I did this

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

but because I need only the first image and not all I found the solution here

I just changed the second @foreach to use slice here is the final code

@foreach ($properties as $property)
      // access property properties here
      @foreach($property->propImages->slice(0, 1) as $image)
        // access image properties here.
      @endforeach
    @endforeach

Change this line:

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

to

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

and try again.

Note: The second argument passed to View::make is an array of data that should be made available to the view.

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM