Commit 1289b5eb authored by Marcos Albano's avatar Marcos Albano 💬

Adicionado os metodos OneToMany e ManyToOne ao controller OneToManyController

parent fcc7b261
......@@ -2,56 +2,42 @@
namespace App\Http\Controllers;
use App\Models\Gleba;
use App\Models\Lote;
use App\Models\Municipio;
use App\Models\Titulo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class OneToManyController extends Controller
{
public function oneToMany()
{
// //$municipio = Municipio::where('uf', 'CE')->get()->first();
// $municipios = Municipio::whereIn('nome', ['ABAIARA', 'ACARAÚ'])->with('lotes')->get();
// //dd($municipios);
// foreach ($municipios as $municipio) {
// echo "<br><b>{$municipio->nome}</b>";
// $lotes = $municipio->lotes()->get();
// $lotes = $municipio->lotes;
// foreach ($lotes as $lote) {
// echo "<br>{$lote->numero}";
// echo " - {$lote->proprietario}";
// echo " - {$lote->cpf}";
// }
// echo "<hr>";
// }
// $municipios = Municipio::get();
// foreach($municipios as $municipio) {
// $lotes = Lote::where('municipio_id', '=',"{$municipio->id}")->with('titulos')->get();
// //dd($municipio);
// foreach ($lotes as $lote) {
// $lotes = Lote::where(str_replace(array('.', '-'), '', "{$lote->cpf}"))->with('titulos')->get();
// dd($lote);
// echo "<br>Proprietário: {$lote->proprietario}";
// echo "<br>CPF: - {$lote->cpf}";
// echo "<hr>";
// $titulos = $lote->titulos()->get();
// $titulos = $lote->titulos;
// foreach ($titulos as $titulo) {
// echo "<br>Número Título: {$titulo->numero_titulo}";
// }
// }
// }
// RELACIONAMENTO MUNICIPIO/LOTE
//$municipios = Municipio::where('id', 2304277)->get()->first();
//$municipios = Municipio::where('uf', "CE")->orderBy('nome', 'asc')->with('lotes')->get();
$municipios = Municipio::where('nome', "ERERÊ")->orderBy('nome', 'asc')->with('lotes')->get();
//dd($municipios);
foreach ($municipios as $municipio) {
//echo "<hr>{$municipio->nome}";
// $lotes = $municipio->lotes()
// ->where('cpf', '<>', null)->get();
$lotes = $municipio->lotes;
//dd($lotes);
foreach ($lotes as $lote) {
//dd($lote);
//echo "<hr>{$lote->numero}";
}
}
}
public function manyToOne()
{
$loteNumero = '0002';
$lote = Lote::where('numero', $loteNumero)->get()->first();
$municipio = $lote->municipio;
//dd($municipio);
//echo "<hr>{$municipio->nome}";
}
}
......@@ -24,9 +24,9 @@ class Lote extends Model
return $results;
}
public function titulos()
public function municipio()
{
return $this->hasOne(Lote::class, 277720 );
return $this->belongsTo(Municipio::class);
}
}
......@@ -20,14 +20,16 @@ class Municipio extends Model
return $results;
}
public function gleba()
{
return $this->hasOne(Glebas::class);
}
// public function gleba()
// {
// return $this->hasOne(Glebas::class);
// }
public function lotes()
{
return $this->hasMany(Lote::class);
//CASO FOSSE VINCULAR COM SITUAÇÃO JURÍDICA (ESSE id SERIA O id DA TABELA sige.situacoes_juridicas) - AULA 9
// return $this->hasMany(Lote::class, 'municipio_id', 'id');
}
}
......@@ -3,6 +3,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
Route::get('many-to-one', 'OneToManyController@manyToOne');
Route::get('one-to-many', 'OneToManyController@oneToMany');
Route::get('one-to-one', 'OneToOneController@oneToOne');
Route::any('agendamentos/search', 'AgendamentoController@search')->name('agendamentos.search')->middleware('auth');//->middleware('auth');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment