msabiry
234
32
Laravel

All other crud working except Update

Posted 1 week ago by msabiry

Hello dear Please help me how can I fix this issue My code won't work on update Controller:

<?php

namespace App\Http\Controllers;

use App\Company;
use App\Customer;
use Intervention\Image\Facades\Image;

class CustomerController extends Controller
{

    // Extends middleware('Auth')
    // Only Authorized Users can access this routes
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $activeUser = Customer::active(); // Active is a scope from Model
        $inactiveUser = Customer::inactive(); // Inactive is a scope from Model
        $companies = Company::all();

        return view('customer', compact('activeUser', 'inactiveUser', 'companies'));
    }

    // Create Records
    public function create()
    {
        $companies = Company::all();
        $customer = new  Customer();
        return view('customer', compact('companies', 'customer'));
    }

    // Save Records
    public function store()
    {
        $customer = Customer::create($this->validateRequest()); // Getting data from validateRequest() function

        $this->storeImage($customer);

//        event(new NewCustomerHasRegisteredEvent($customer));

        return back()->with('success', 'Added Successfully!');
    }

    // Show Records
    public function show(Customer $customer)
    {
        $companies = Company::all();
        return view('profile', compact('customer', 'companies'));
    }

    // Edit Records
    public function edit(Customer $customer)
    {
        $companies = Company::all();
        return view('profile', compact('customer', 'companies'));
    }

    // Update Records
    public function update(Customer $customer)
    {
        $customer->update($this->validateRequest());

        $this->storeImage($customer);

        return back();
    }

    // Delete Records
    public function destroy(Customer $customer)
    {
        $customer->delete();
        return redirect('customers');
    }

    public function validateRequest()
    {
        return tap(request()->validate([
            'fName' => 'required|max:24',
            'lName' => 'required|max:24',
            'email' => 'required|max:64|email|unique:customers',
            'message' => 'nullable',
            'active' => 'required',
            'company_id' => 'required',
        ]), function (){
            if (request()->has('image')){
                request()->validate([
                    'image' => 'file|image|max:5000'
                ]);
            }
        });
    }

    public function storeImage($customer)
    {
        if (request()->has('image')){
            $customer->update([
                'image' => request()->image->store('images', 'public'),
            ]);

            $image = Image::make(public_path('storage/' . $customer->image))->fit(60, 60, null, 'top-left');
            $image->save();
        }
    }
}

my Blade:

<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="editLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <p class="modal-title" id="editLabel" style="font-size: medium; font-weight: bold;">Edit</p>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form class="form-group" method="post" enctype="multipart/form-data" action="{{ route('customers.update', ['customer' => $customer]) }}">
                @method('PATCH')
                @csrf
                <div class="modal-body">
                    <div class="form-group">
                        <label for="fName" class="col-form-label">First Name:</label>
                        <input type="text" name="fName" class="form-control" placeholder="First Name" style="border-bottom: 2px; font-size: small;" value="{{ old('fName') ?? $customer->fName }}">
                    </div>

                    <div class="form-group">
                        <label for="fName" class="col-form-label">First Name:</label>
                        <input type="text" name="lName" class="form-control" placeholder="Last Name" style="border-bottom: 2px; font-size: small;" value="{{ old('lName') ?? $customer->lName }}">
                    </div>

                    <div class="form-group">
                        <label for="text" class="col-form-label">Message:</label>
                        <textarea name="message" id="message" class="form-control" style="border-bottom: 2px; font-size: small;" placeholder="Message...">{{ old('message') ?? $customer->message }}</textarea>
                    </div>
                    <div class="form-group">
                        <label for="active" class="col-form-label">Status:</label>
                        <select name="active" id="active" class="form-control">
                            <option value="" disabled>Select an option</option>
                            @foreach($customer->activeOptions() as $activeOptionKey => $activeOptionValue)
                                <option value="{{ $activeOptionKey }}" {{ $customer->active == $activeOptionValue ? 'selected' : '' }}>
                                    {{ $activeOptionValue }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="company_id" class="col-form-label">Company:</label>
                        <select name="company_id" id="company_id" class="form-control">
                            @foreach($companies as $company)
                                <option value="{{ $company->id }}"
                                        {{ $company->id == $customer->company_id ? 'selected' : '' }}>
                                    {{ $company->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group d-flex flex-column">
                        <label for="image" class="col-form-label">Profile Picture:</label>
                        <input type="file" accept="image/*" name="image" class="py-2">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Save</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </form>
        </div>
    </div>
</div>

my route:

Route::resource('customers', 'CustomerController');

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.