Created
March 23, 2017 18:14
-
-
Save tiagoandrepro/c4f04b992ba85f98aee3f4ea9378ea69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Web; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
use App\Models\Establishment; | |
use App\Models\Nature; | |
use App\Models\Specialty; | |
use Illuminate\Support\Facades\DB; | |
class SearchController extends Controller | |
{ | |
public function search(Establishment $establishment) | |
{ | |
$search = \Request::get('result'); | |
if ($search) { | |
//$establishmens = $establishment->all(); | |
$establishmens = $establishment::where('nmFantasia', 'like', '%' . $search . '%')->paginate(25); | |
return view('web.search', compact('establishmens', 'search')); | |
} else { | |
return 'Nenhum termo digitado'; | |
} | |
} | |
/*public function specialty() | |
{ | |
/*$search = \Request::get('result'); | |
if ($search) { | |
$users = DB::table('estabelecimento') | |
->select('nmFantasia', 'nmEspecialidade') | |
->join('especialidade_estabelecimento', 'idUnidade', '=', 'estabelecimento_idUnidade') | |
->join('especialidade', 'idEspecialidade', '=', 'especialidade_idEspecialidade') | |
->get(); | |
foreach ($users as $user) { | |
echo " {$user->nmFantasia}<br> "; | |
echo "<b>{$user->nmEspecialidade}</b><br>"; | |
} }else { | |
return 'Nenhum termo digitado'; | |
} | |
}*//* | |
$search = \Request::get('result'); | |
if ($search) { | |
$especialidade = Specialty::where('nmEspecialidade', 'like', '%' . $search . '%')->get()->first(); | |
echo "{$especialidade->nmEspecialidade}<br/>"; | |
$unidades = $especialidade->establishments; | |
foreach ($unidades as $unidade) { | |
echo "{$unidade->nmFantasia}<br/>"; | |
} | |
} else { | |
return 'Nenhum termo digitado'; | |
} | |
}*/ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
use App\Models\Establishment; | |
use App\Models\Nature; | |
use App\Models\Specialty; | |
class Establishment extends Model | |
{ | |
// | |
public $table = "estabelecimento"; | |
protected $fillable = ['nmFantasia']; | |
protected $primaryKey = 'idUnidade'; | |
public function specialtys () { | |
//return $this->belongsToMany(Specialty::class, 'especialidade_estabelecimento', 'especialidade_idEspecialidade', 'estabelecimento_idUnidade'); | |
return $this->belongsToMany('App\Models\Specialty', 'especialidade_estabelecimento', 'especialidade_idEspecialidade', 'estabelecimento_idUnidade'); | |
} | |
public function type () { | |
//return $this->belongsToMany(Specialty::class, 'especialidade_estabelecimento', 'especialidade_idEspecialidade', 'estabelecimento_idUnidade'); | |
return $this->hasOne('App\Models\tipo'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class Type extends Model | |
{ | |
public $table = "tipoestabelecimento"; | |
protected $primaryKey = 'idTipoEstabelecimento'; | |
protected $fillable = ['nmTipoEstabelecimento']; | |
public function establishment () { | |
return $this->hasMany('App\Models\Estabelecimento', 'tipoestabelecimento', 'tipoestabelecimento_idTipoEstabelecimento'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
O relacionamento tem que ser assim, pois o nome da coluna da FK não corresponde ao padrão que o laravel suporta: