Jak zrobić update bez zmian wartości pól unikalnych ?

0

Próbuję zrobić update na użytkowniku. Gdy mam formularz do update to jest wypełniony aktualnymi danymi. Do tego robię walidację i tutaj jest problem bo jak aktualizuję i nie zmienię email i numeru telefonu to pojawiał się błąd że taki email lub numer już istnieje. Dlatego dodałem taki kod Rule::unique('customers')->ignore($this->customer) ale teraz gdy wpiszę email innego usera to nie pojawia się błąd. W skrócie chodzi o to żeby przy update gdy nie chcę zmieniać email nie pojawiał się błąd walidacji, ale pojawiał się gdy zmienię email na taki który juz istnieje w bazie.

Oto mój kod:

class CustomerRequest extends FormRequest
{
    public function rules()
    {
        return [
            'first_name'   => 'min:3|max:20|regex:/^[a-zA-z-0-9]+$/u',
            'last_name'    => 'min:3|max:30|regex:/^[a-zA-z-0-9]+$/u',
            'email'        => 'email', Rule::unique('customers')->ignore($this->customer),
            'phone_number' => 'min:9|max:9', Rule::unique('customers')->ignore($this->customer)

        ];
    }

Kontroler

public function update(CustomerRequest $request, Customer $customer)
{
    $data = $request->validated();
    $customer->fill($data);
    $customer->save();

    return redirect()->route('customers.index')->with('updateMessage', 'Customer data successfully updated');
}
0

Skąd masz $this->customer ? Podejrzewam, że tam masz nulla.

0

@ehhhhh: Zmodyfikowałem tą funkcję, teraz nie ma błędu. Teraz gdy robię update i nie zmieniam email to nie ma błędu walidacji że email jest zajęty, ale gdy zmieniam email na taki który jest już zajęty to też błędu walidacji nie ma a powinien być

public function rules(Customer $customer)
{
    return [
        'first_name'   => 'min:3|max:20|regex:/^[a-zA-z-0-9]+$/u',
        'last_name'    => 'min:3|max:30|regex:/^[a-zA-z-0-9]+$/u',
        'email'        => 'email', Rule::unique('customers')->ignore($customer->id) 
    ];
0

'email', Rule::unique('customers')->ignore($customer->id) to powinno być tablicą więc ['email', Rule::unique('customers')->ignore($customer->id)]

0

@ehhhhh:

Zrobiłem tak 'email' => ['email', Rule::unique('customers')->ignore($customer->id)], ale gdy robię update i nie zmieniam email to jest błąd walidacji że email jest zajęty

0

czyli wracamy do punktu wyjścia o którym ci pisałem czyli do ignore trafia null a nie int

0

@Joan: Źle robisz tą walidację. Zrób coś takiego:

return [
        'email' => 'required|email|unique:customers,email,'. $customerId.',id',
    ];

customerId przekaż w polu input typu hidden

0

Po mojemu

'email' => 'required|email|unique:customers,email,'.$this->customer()->id,

i ma śmigać nie ma że boli.

0

@ehhhhh No tak, racja. Wkurzyłem się i zrobiłem test i wyszło mi na to, że albo formularz musi mieć pole hiddenz id albo id użytkownika brać parametru ze ścieżki Route w każdym to id' w kontrolerze i w CustomerRequest będzie potrzebne

Kontroler:

        $data = $request->validated();
        $customer = Customer::where('id',  $request->id)->firstOrFail();
        $customer->fill($data);
        $customer->update();

CustomerRequest:

'email'  => 'required|email|unique:customers,email,'.$this->id,
0
Rule::unique('customers')->ignore($request->customer->id)

moze to zadziala

0

@kAzek: on korzysta z routemodelbinding więc w kontrolerze ma już obiekt przez DI ale w request musi pobrać po prostu parametr czyli id bo tam ten obiekt nie istnieje,

0

No jak nie istnieje, przeciez jest router na ('customer.update', [$customer) a w route masz Route::any('/customer/update/{customer}', [CustomerController::class, 'update'])->name('customer.update'); widzisz ze w kontrolerzze odbiera custom request i customer model — chomikowski 2023-01-16 12:42
w requeście tego tak nie odczytasz — ehhhhh 2023-01-16 13:47

oto moj kod

public function details(Request $request, Orders $order)
    {
        dd($request->order);
        return view('orders.details', [
            'order' => $order,
        ]);
    }

jak skasujesz Orders $order to wyswietli ci tylko nr zamowienia jaki jest przekazywany nic wiecej. Order i inne paramsy tez leca przez request, nie ma oddzielnego kanalu ze requestem idzie costam a czyms innym costam, wszystko masz w requescie

1 użytkowników online, w tym zalogowanych: 0, gości: 1