2017-10-07 121 views
5

Eğer laravel eloquent bir satırı güncellemede sadece bir sorgu çalıştırıyorsa, merak ettim. Bu yüzden Bu bana Laravel Eloquent Güncellemede iki sorguyu çalıştırır mı?

{ 
    "client": "Noob", 
    "query": [{ 
       "query": "select * from `clients` where `clients`.`id` = ? limit 1", 
    "bindings": [ 
      1 
     ], 
    "time": 0.71 
}, 
{ 
     "query": "update `clients` set `first_name` = ?, `updated_at` = ? where `id` = ?", 
     "bindings": [ 
      "Noob", 
      "2017-10-07 12:03:05", 
      1 
     ], 
     "time": 3.36 
} 
] 
} 

Yani DB Cephe kullanma hakkında düşünce şu sonucu verdi aşağıdaki

Route::get('/cli', function(){ 
    DB::enableQueryLog(); 

    $client = Client::findOrFail(1); 
    $client->first_name = 'Noob'; 
    $client->save(); 
    return response()->json([ 
     'client' => $client->first_name, 
     'query' => DB::getQueryLog() 
    ], 200); 
}); 

çalıştı.

Route::get('/cli', function(){ 
    DB::enableQueryLog(); 
$client = DB::select("update clients set first_name= 'Admin test' WHERE id=1"); 

    return response()->json([ 
     'client' => $client->first_name, 
     'query' => DB::getQueryLog() 
    ], 200); 
}); 

ve sonuç o İstemci örneğini iade etmedi gerçi Bu seferki sadece bir sorgu koştu

{ 
"client": [], 
"query": [ 
{ 
"query": "update clients set first_name= 'Admin test' WHERE id=1", 
"bindings": [], 
"time": 3.2 
} 
] 
} 

olduğunu. Sorum şu, kullanmak için hangi yöntem veya stratejinin gerekli olduğu yüksek trafikli bir sunucu için? Kullanmaya elverişli başka daha iyi teknikler veya püf noktaları var mı?

i kesinlikle iki sorgu çalıştırır böylece değerinin güncellenmesi sonra seçerek ilk sorgusunda sonra Sorunuzu doğru anlamış ve varsa Sen

$client = DB::select("update clients set first_name= 'Admin test' WHERE id=1"); 

için

$data=['first_name'=>'Admin test']; 
    Client::where('id', '=', 1)->update($data); 

anlamlı sorgunun altında değiştirebilir

cevap

4

. İkinci yönteminizde, db'den herhangi bir düşünce almıyorsunuz, bu nedenle ilk

+0

'dan başka bir kısa soru. DB :: select() eloquent'den daha mı hızlı? –

+1

Performans sorununda çok fark yaratacağını düşünmüyorum. Daha hızlı performans verebileceği büyük birleşim sorguları. – iCoders