V
V
Vanya Huk2018-09-21 14:35:47
Laravel
Vanya Huk, 2018-09-21 14:35:47

How to check the values ​​of all filenames for uniqueness?

there is an array:

array:5 [
  0 => array:4 [
    "preview" => "blob:http://127.0.0.1:8000/d8d6b9e3-97e2-46c7-a2e9-8f05f9f59b63"
    "uploaded" => 1
    "filename" => "5ba4d101048ed_887ef694391f92971f6358c671c55e27.jpg?1537528066083"
    "isCover" => 1
  ]
  1 => array:3 [
    "preview" => "blob:http://127.0.0.1:8000/c83d8037-a0da-412e-b7ea-4872e7d33237"
    "uploaded" => 1
    "filename" => "5ba4d10228059_857336b00f3eefefdfefa3339416ebf9.jpg?1537528067092"
  ]
  2 => array:3 [
    "preview" => "blob:http://127.0.0.1:8000/2c1ccb68-a578-4f88-8d57-30a1072b5d5b"
    "uploaded" => 1
    "filename" => "5ba4d10321b3a_33197f8397faa75bb157b9e784f2f128.jpg?1537528068000"
  ]
  3 => array:3 [
    "preview" => "blob:http://127.0.0.1:8000/4b946662-90a6-4b28-94e0-e4c47627c35e"
    "uploaded" => 1
    "filename" => "5ba4d104d419a_74310cd34625bf5dc189f3adcfd7f61a.jpg?1537528069764"
  ]
  4 => array:3 [
    "preview" => "blob:http://127.0.0.1:8000/6457157d-bfb2-4ee9-a3ed-333e9c900bb1"
    "uploaded" => 1
    "filename" => "5ba4d10409a0e_becb3d4e554cef42eb5d8261f392d288.jpg?1537528068828"
  ]
]

I need to use the Laravel Validator to check each filename field for uniqueness in the pictures (id, name) table and for the existence of a file in the system, how can I do this while using a minimum of code?
PS: There was an idea to collect the names first, then do a whereIn in the database, then collect only the unique names and then check them for their presence in the file system, maybe someone has better solutions to the problem.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-09-21
@Astatroth

Have you tried the rule ? 'fields.*.filename' => 'unique'

A
Artyom, 2018-09-21
@dark1112

'array.*.filename' => 'unique:table,column'
Checks for uniqueness against the column field of the table table in the database.
But with the presence of a standard method in the file system, you need to write your own

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question