Laravel — P47: Final Touchups (CMP)

Give your Laravel project the perfect finishing touches

It’s time to wrap this project up and move on to more advanced functionality in Laravel. Before we do, we need to add a couple of additional features to our Car Management Project, like deleting photos. This was a fun little project and in my opinion a little better than a traditional to-do list.

Displaying All Images on the Show Page

There is an issue with our show page. I’m sure you’ve caught that already. When we were just adding URL’s to other sites, it was easy to display the image.

@foreach( $car->images as $image )
    <div class="w-80 bg-white p-3">
        <img class="h-52 w-full object-cover" src="{{ $image->url }}" alt="{{ $image->alt }}" />
    </div>
@endforeach

The problem now is that we’re hosting our own images and need to change the src content. I’ll quickly edit one of the pages and upload 6 total images. I’ll get the images from https://www.pexels.com/.

Going to our show view for this particular vehicle diplays a ton of broken image links.

The main image works, since we wrote it the right way, but the bottom images are broken. We simply need to use our asset() helper in order to fix them.
@foreach( $car->images as $image )
    <div class="w-80 bg-white p-3">
        <img class="h-52 w-full object-cover" src="{{ asset( 'storage/' . $image->url ) }}" alt="{{ $image->alt }}" />
    </div>
@endforeach
Refreshing the page yields the results that we wanted to see.

Deleting Images

We’re not going to cover deleting individual images just yet; we’ll tackle that at a later time. For now, we want to remove the images from our tables and from the storage.

This will all be done in our PersonalCarController::destroy method.

public function destroy($id)
{
    PersonalCar::destroy($id);

    return redirect()->to('/personalcars/')->with('status', 'Your car has been deleted.');
}

Before we destroy the resource, we need to remove the allocation and image.

public function destroy($id)
{
    $car = PersonalCar::find($id);

    $car->images()->delete();
    $car->images()->detach();
    $car->destroy($id);

    return redirect()->to('/personalcars/')->with('status', 'Your car has been deleted.');
}

This piece of code deletes the image from the images table and removes the relationship from our image_personal_car pivot table. It finally removes the record from our personal_cars table.

We are still left with the image existing though. We need to remove the image(s) from storage. We can use the Storage facade to remove the image from our public disk.

<?php

namespace App\Http\Controllers;

// ...
use App\Models\PersonalCar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PersonalCarController extends Controller
{
    // ...

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     */
    public function destroy($id)
    {
        $car = PersonalCar::find($id);

        foreach($car->images as $image) {
            Storage::disk('public')->delete( $image->url );
        }

        $car->images()->delete();
        $car->images()->detach();
        $car->destroy($id);

        return redirect()->to('/personalcars/')->with('status', 'Your car has been deleted.');
    }
}

That’s it. We’re finally done with the Car Management Project. Time to move on to bigger and better things.

Laravel Series

Continue your Laravel Learning.

Laravel — P46: Controller Destroy (CMP)

Simplifying Data Removal with Laravel’s Controller Destroy Method

Laravel – P46: CMP – Controller Destroy

Learn how to use the Destroy method in Laravel controllers to efficiently handle data deletions and maintain clean data management.

Laravel — P47: Final Touchups (CMP)

Give your Laravel project the perfect finishing touches

Laravel – P47: CMP – Final Touchups

In part 47 of our Laravel series, wrap up your application by applying crucial final adjustments. Learn best practices for code refactoring, caching strategies, QA checks, and polishing UI elements to ensure a secure, high-performing, and fully optimized Laravel project.

Leave a Reply