Question:
Checking records present in another database- Laravel

Solution

Let’s assume, we take ->get() that returns a Collection, which even if empty, is always "truthy", so using it in a ternary won't work.


Use isEmpty() to return true or false explicitly:


$author->quotes = $quotes->isEmpty() ? null : 'https://www.example.com/authors/' . $author->slug';


Or, you can simply use exists() instead:


quotes = DB::connection('quotes')->table('frasidautore.authors')->where('slug', 'LIKE', '"%{$author->slug}"')->exists();


$author->quotes = $quotes ? 'https://www.example.com/authors/' . $author->slug' : null;


Exists() can be used in a ternary without raising issues with implied "truthyness" because it returns true or false depending on the conditions that came before it.


Answered by:>> >Tim Lewis

Credit:>> >StackOverflow


Blog Links

>How to manage the Text in the container in Django?

>Fix webapp stops working issue in Django- Python webapp

>Creating a form in Django to upload a picture from the website

>Sending Audio file from Django to Vue

>How to keep all query parameters intact when changing page in Django?

>Solved: TaskList View in Django

>Implement nested serializers in the Django rest framework

>How to filter events by month in Django?

>Sorting the restframework in Django

>Ways to access instances of models in view in order to save both forms at once in Django

>What makes index.html have such kind of name in Django?

>Fix Module Not Found during Deployment- Django

>Creating a Django with existing directories, files, etc.?

>How to Read a CSV file with PHP using cURL?


Nisha Patel

Nisha Patel

Submit
0 Answers