?
Current File : /home/sanghavirealty/www/appazwer/Http/Controllers/PropertyImageController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\PropertyImage;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;


class PropertyImageController extends Controller
{
    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'property_id' => 'required|exists:properties,id',
            'image_file' => 'required|array',
            'image_file.*' => 'mimes:jpeg,jpg,png,pdf|max:10240',
            'type' => 'required|string',
        ]);

        $propertyId = $request->input('property_id');
        $type = $request->input('type');

        if ($request->hasFile('image_file')) {
            foreach ($request->file('image_file') as $file) {
                // Corrected path to use the 'property_images' folder
                $path = $file->store('public/property_images');

                PropertyImage::create([
                    'property_id' => $propertyId,
                    'file_path' => $path,
                    'type' => $type,
                    'primaryimage' => false,
                    'featured' => false,
                    'highlight' => false,
                ]);
            }
        }

        return redirect()->back()->with('success', 'Images uploaded successfully!');
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, PropertyImage $propertyImage)
    {
        $action = $request->input('action');

        switch ($action) {
            case 'toggle-primary':
                // Reset other primary images for this property
                PropertyImage::where('property_id', $propertyImage->property_id)
                    ->where('id', '!=', $propertyImage->id)
                    ->update(['primaryimage' => false]);
                $propertyImage->primaryimage = !$propertyImage->primaryimage;
                $propertyImage->save();
                return redirect()->back()->with('success', 'Primary image status updated.');

            case 'toggle-highlight':
                $propertyImage->highlight = !$propertyImage->highlight;
                $propertyImage->save();
                return redirect()->back()->with('success', 'Highlight status updated.');

            case 'toggle-featured':
                $propertyImage->featured = !$propertyImage->featured;
                $propertyImage->save();
                return redirect()->back()->with('success', 'Featured status updated.');

            default:
                return redirect()->back()->with('error', 'Invalid action.');
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(PropertyImage $propertyImage)
    {
        Storage::delete($propertyImage->file_path);
        $propertyImage->delete();

        return redirect()->back()->with('success', 'Image deleted successfully!');
    }
}