I hope this time around I have been to clarify any misunderstanding from the previous post. If there is anything that could use further clarification please tag team and I will do my best to explain.
Another Shot at Clarity
I wanted to make a new post about describing what I’m trying to achieve and help explain the parts of the app that are pertinent to my goal. This application has Properties, Rooms (Kitchen, Living Room, Bedroom, etc.), A Property can have many types of Rooms. When an inspector goes to a property they can make observations about each room in the property. Observations can be either a Test (different types of tests are Mold, Air, etc.), an Image (pictures taken of the room during the observation), Comment (further text describing the observation of the room).
What I'm trying to achieve is figuring out how I save an image for a property_room.
My current database structure is the following.
PROPERTY_ROOM
id
property_id
room_id
OBSERVATIONS
id
property_room_id
observation_id
observation_type
OBSERVATION_PHOTOS
OBSERVATION_TESTS
OBSERVATION_COMMENTS
I have the following models to help with this.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class PropertyRoom extends Pivot
{
//
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ObservationImage extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['url', 'sort_order'];
/**
* Get the observation associated to the image.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function observation()
{
return $this->morphedToMany(Observation::class, 'observable');
}
}
The following test I’m trying to upload an image to be assigned to a property_room but will also be saved as a morphed type for the observations table.
/** @test */
public function an_administrator_can_upload_an_image_as_an_observation()
{
$this->withoutExceptionHandling();
Storage::fake('s3');
$file = UploadedFile::fake()->image('observation.jpg');
$signedInUser = factory(User::class)->states('administrator')->create();
$observation = factory(Observation::class)->create();
$response = $this->actingAs($signedInUser)->json('POST', '/observations/'.$observation->id.'/images', $this->validParams([
'image' => $file,
]));
$response->assertStatus(200);
tap($observation->images->first(), function ($image) use ($observation, $file) {
dd($image->observation);
$this->assertEquals('observations/'.$observation->id.'/'.$file->hashName(), $image->url);
$this->assertEquals(1, $image->sort_order);
});
}
The code needs modified to work toward the desired solution.
<?php
namespace App\Http\Controllers;
use App\Models\Observation;
use App\Jobs\ProcessObservation;
use App\Http\Requests\StoreObservationImageRequest;
use App\Http\Resources\Observation as ObservationResource;
class ObservationImagesController extends Controller
{
/**
* @param \App\Http\Requests\StoreObservationImageRequest $request
* @param \App\Models\Observation $observation
* @return
*/
public function store(StoreObservationImageRequest $request, Property $property, Room $room)
{
$path = request()->file('image')->store('property-rooms/images');
$file = Storage::disk('s3')->put('property-rooms/', request()->image, 'public');
$max = $this->observation->images()->max('sort_order');
$this->observation->images()->create([
'url' => $file,
'sort_order' => $max + 1,
]);
return new ObservationResource($observation);
}
}
Please or to participate in this conversation.