Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [LARAVEL] Validacja w stylu unique jezeli
fumfel20
post 19.10.2019, 10:02:16
Post #1





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 17.09.2015

Ostrzeżenie: (0%)
-----


Witam, mam taka tabele gdzie sa kolumny
id
inventory_no
part_no
transaction

numer inventory jest unikalny dla kazdego produktu i nie moze wystapic dwa razy w tabeli ALE jezeli transaction jest takie samo. Transaction jest zalezne od miejsca gdzie produkt jest skanowany i moze to wygladac tak:
id - 1
inventory_no -12345678
part_no - product_1
transaction - fabryka_1

id - 2
inventory_no -12345678
part_no - product_1
transaction - fabryka_2

I tak jest ok, natomiast validacja powinna byc w stanie zatrzymać insert jezeli wystapi cos takiego:
id - 1
inventory_no -12345678
part_no - product_1
transaction - fabryka_1

id - 2
inventory_no -12345678
part_no - product_1
transaction - fabryka_1
Czyli ten sam inventory z ta samą transaction.
Wiem ze w metodzie moge zrobic prostego IFa ktory mi to sprawdzi ale zastanawiam sie czy nie mozna tego zrobic we wbudowanej walidacji Laravel:
  1. public function store(Request $request)
  2. {
  3. $this->validate($request, [
  4. 'title' => 'required|unique:posts|max:255',
  5. 'body' => 'required',
  6. ]);
  7. }


Ten post edytował fumfel20 19.10.2019, 10:03:09
Go to the top of the page
+Quote Post
markonix
post 19.10.2019, 10:06:45
Post #2





Grupa: Zarejestrowani
Postów: 2 707
Pomógł: 290
Dołączył: 16.12.2008
Skąd: Śląsk

Ostrzeżenie: (0%)
-----


https://laravel.com/docs/6.x/validation#rule-unique

na samym końcu jest przykład z dodatkowym where.


--------------------
Go to the top of the page
+Quote Post
fumfel20
post 19.10.2019, 12:30:48
Post #3





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 17.09.2015

Ostrzeżenie: (0%)
-----


Dzieki, szukalem raczej czegos takiego:
  1. $request->validate([
  2. 'inventory_no' => 'required|unique:history,inventory_no,'. $request->request->get('inventory_no'). ',transaction,'. $request->request->get('transaction'),
  3. 'transaction' => 'required',
  4. ]);
  5. History::create($request->all());


Mimo ze to nie dziala biggrin.gif Potem chcialem przeniesc tę validacje do nowo utworzonego Requestu dla tego formularza zeby w store zostawic tylko
  1. History::create($request->all());




EDIT:
Zmienilem troche walidacje i prawie jestem w domu. Teraz to wyglada tak:
  1. $request->validate([
  2. 'inventory_no' => 'required|unique:history,inventory_no,transaction,'. $request->request->get('transaction')
  3. ]);


Komunikat bledu:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'A-1' in 'where clause' (SQL: select count(*) as aggregate from `history` where `inventory_no` = 12345678 and `A-1` <> transaction)


Czyli dodawany jest nastepny argument AND ale sprawdza kolumne A-1 zamiast transaction, jak to odwrocic?

Ten post edytował fumfel20 19.10.2019, 12:43:17
Go to the top of the page
+Quote Post
viking
post 19.10.2019, 12:49:48
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Zrób jak pokazują w dokumentacji. Reguły poszczególne jako tablica, dodajesz unique i zapytania where które chociaż poprawnie zabezpieczone będą.


--------------------
Go to the top of the page
+Quote Post
fumfel20
post 19.10.2019, 13:02:56
Post #5





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 17.09.2015

Ostrzeżenie: (0%)
-----


Albo uzyc tego:
https://github.com/felixkiss/uniquewith-validator

Udalo sie smile.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 22:15