Мое решение сравнивает объекты, а не массивы. Это будет работать так же, как Tomáš as Arrays - это объекты, но без предупреждения:
Object.prototype.compare_to = function(comparable){
// Is the value being compared an object
if(comparable instanceof Object){
// Count the amount of properties in @comparable
var count_of_comparable = 0;
for(p in comparable) count_of_comparable++;
// Loop through all the properties in @this
for(property in this){
// Decrements once for every property in @this
count_of_comparable--;
// Prevents an infinite loop
if(property != "compare_to"){
// Is the property in @comparable
if(property in comparable){
// Is the property also an Object
if(this[property] instanceof Object){
// Compare the properties if yes
if(!(this[property].compare_to(comparable[property]))){
// Return false if the Object properties don't match
return false;
}
// Are the values unequal
} else if(this[property] !== comparable[property]){
// Return false if they are unequal
return false;
}
} else {
// Return false if the property is not in the object being compared
return false;
}
}
}
} else {
// Return false if the value is anything other than an object
return false;
}
// Return true if their are as many properties in the comparable object as @this
return count_of_comparable == 0;
}
Надеюсь, это поможет вам или кому-либо еще найти ответ.
Спасибо. Йонас-Стауденмейр. Он помогает мне найти решение.
В этом случае мы можем использовать метод selectSub()
из класса построителя запросов laravel. selectSub ()
Для первого:
$user_email = DB::table('user_detail')
->select('user_detail.user_email')
->whereRaw('user_detail.user_id = user.id')
->take(1);
$data = DB::table('user')
->select('name', 'age')
->selectSub($user_email, 'email') // selectSub(sub_query, as)
->get();
Для второго:
//session query
$session = DB::table('session')
->select('session.name')
->whereRaw('transaction.session_id = session.id')
->take(1);
//from_account query
$from_account = DB::table('account')
->select('account.name')
->whereRaw('transaction.from_account_id = account.id')
->take(1);
//to_account query
$to_account = DB::table('account')
->select('account.name')
->whereRaw('transaction.to_account_id = account.id')
->take(1);
//action query
$action = DB::table('action')
->select('action.action_name')
->whereRaw('transaction.action_table_id = action.id')
->take(1);
//another_table query
$another_table = DB::table('another_table')
->select('another_table.detail')
->whereRaw('transaction.another_table_id = another_table.id')
->take(1);
$data = DB::table('transaction')
->select('transaction.id as transaction_id', 'other_attributes')
->selectSub($session, 'session_name') //session as session_name
->selectSub($from_account, 'from_account_name') //from_account as from_account_name
->selectSub($to_account, 'session_name') //to_account as to_account_name
->selectSub($action, 'action_name') //action as action_name
->selectSub($another_table, 'another_table_detail') //another_table as another_table_detail
->get();
Хотя мы можем использовать соединение или левое соединение. Какой из них быстрее, все зависит от данных, индексов, корреляции, количества данных, запросов и т. Д. Подзапросы могут быть медленнее, чем LEFT [OUTER] JOINS, но, на мой взгляд, их сила немного выше читаемости. нарисуй против-суб-запроса
Попробуйте это
$users = User::join('users', 'users.id', '=', 'user_detail')->get();
Следует объединить две таблицы со всеми необходимыми записями.