Answer the question
In order to leave comments, you need to log in
What is the best way to check records in an array for uniqueness?
Generally there is an array of 10 records, there is a table of 10000 records. It is necessary to determine which records in the array are not in the table and then insert them. Before this I had
$select_all_news = DB::select("select * from news WHERE author='автор'");
for ($x=0; $x<count($all_news['url']); $x++)
{
if(!DB::table('news')->where('source',$all_news['url'][$x])->where('author',$author)->first())
{
$unic_news[$x]['url']=$all_news['url'][$x];
$unic_news[$x]['preview_img']=$all_news['preview_img'][$x];
$unic_news[$x]['title']=$all_news['title'][$x];
$unic_news[$x]['small_description']=$all_news['small_description'][$x];
}
}
Answer the question
In order to leave comments, you need to log in
The second option has the right to exist. Additionally, I would advise you to add indexes to the bunch of fields "source" and "author" to speed up the search. And also not to do ->first()
to check the existence of a record, for this there is a method exists.
Requests in cycles are very bad, it is always desirable to get rid of this.
You must have a unique key in the database so that there are no duplicates. When you do that, you can use insert ignore
Your code can be simplified by using the firstOrCreate method
To get rid of the queries in the loop you need to
1. Query the database whereIn
2. Loop through what is found and filter
3. Add the rest through one insert
This way you will only have two queries
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question