I created a query. I want to join my table with students table:
$query->leftJoin('students', 'learners.student_id', '=', 'students.id');
But I don't know my table joined before or not. How should I do that?
Eloquent ORM is best suited working with fewer data in a particular table. On the other side, query builder takes less time to handle numerous data whether in one or more tables faster than Eloquent ORM. In my case, I use ELoquent ORM in an application with tables that will hold less than 17500 entries.
This allows you to add conditions throughout your code until you actually want to fetch them, and then you would call the get() function.
To enables query log for my_connection : DB::connection('my_connection')->enableQueryLog(); To get query log for my_connection : print_r( DB::connection('my_connection')->getQueryLog() );
I found this solution:
function joined($query, $table) {
$joins = $query->getQuery()->joins;
if($joins == null) {
return false;
}
foreach ($joins as $join) {
if ($join->table == $table) {
return true;
}
}
return false;
}
if ( joined($query, 'students') ) {
$query->leftJoin('students', 'learners.student_id', '=', 'students.id');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With