Nabeel Siddiqui
Nabeel Siddiqui

Reputation: 65

compare all data in two tables in laravel

I have two tables and I want to compare all the values for all columns except for col 4 in table 2 and check whether the data matches or not.

Table 1

Col 1 Col 2 Col 3
1 2 3
4 5 6

Table 2

Col 1 Col 2 Col 3 Col 4
1 2 3 7
4 5 6 8

Is there any way to solve it in laravel? Thank you in advance

Upvotes: 0

Views: 381

Answers (1)

Rateb Habbab
Rateb Habbab

Reputation: 1789

If the first table called books with Book model and the second table called book_copies with BookCopy model, you could try the following:

$books = Book::select('col1', 'col2', 'col3')->get();
$bookCopies = BookCopy::select('col1', 'col2', 'col3')->get();
return $books->diffAssoc($bookCopies)->isEmpty();

Upvotes: 1

Related Questions