Skip to content

Instantly share code, notes, and snippets.

@ph4un00b
Last active December 14, 2015 23:09
Show Gist options
  • Save ph4un00b/5163885 to your computer and use it in GitHub Desktop.
Save ph4un00b/5163885 to your computer and use it in GitHub Desktop.
orm laravel snippets
// encontrar datos, cruzarlos con otra tabla y atualizar
// todo: esto se puede hacer mejor, pero por lo mientras...
$rows1 = DB::table("nombre_tabla1")
->where("nombre_campo_tabla1", "=", "string_buscado")
->get();
foreach ($rows1 as $row1) {
// echo $row1->nombre_campo . "<br>";
//cruzar un dato con otra tabla
$rows2 = DB::table("nombre_tabla2")
->where("mail", "=", $row1->email)
->where("otro_campo", "<>", "alguna_string")
->where("otro_campo_mas", "=", null)
->take(1)
->get();
foreach ($rows2 as $row2) {
// actualizando
$row2->mail = $row1->mail;
$row2->save();
}
}
/**
* update from custom timestamp
*/
$datos = DB::table("tabla")
// ->take(2)
->get();
$index = 0;
foreach ($datos as $dato) {
$index++;
$d = substr($dato->no_v, 1, 2);
$m = substr($dato->no_v, 3, 2);
$y = substr($dato->no_v, 5, 2);
$h = substr($dato->no_v, 7, 2);
$i = substr($dato->no_v, 9, 2);
$s = substr($dato->no_v, 11, 2);
$date = $y . "-" . $m . "-" . $d . " " . $h . ":" . $i . ":" . $s;
// $date = $y . "-" . $m . "-" . $d;
$date = new DateTime($date);
$result = $date->format('Y-m-d H:i:s');
// var_dump($result);
DB::table('tabla')
->where('id', $dato->id)
->update(array('created_at' => $result));
echo $index . "<br>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment