Description The search engine currently makes a query to the news table where it finds the data by ( news_program, noticiero_turno, news_date ), the question is How can you make in that same query extract data from the another table that is called notes? to then show the results in the view.
Models Note table
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Nota extends Model
{
protected $fillable = ['nombre_nota', 'editor_nota','duracion_nota', 'bloque_nota', 'noticia_id'];
public function noticias()
{
return $this->BelongsTo('App\noticia', 'noticia_id');
// pertenece a
}
}
Model table news
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Noticia extends Model
{
protected $fillable = ['noticiero_programa','noticiero_turno','noticiero_fecha'];
public function notas()
{
return $this->hasMany('App\nota', 'noticia_id');
// tiene muchos
}
}
Search view
@extends('layouts.default')
@section('content')
<div class="panel panel-success">
<div class="panel-heading">Buscar</div>
<form action="/noticia/buscar" method="get" onsubmit="return showLoad()">
<div class="panel-body">
<label class="label-control">Buscar</label>
<input type="text" name="noticiero_turno" class="form-control" placeholder="Please input stock name/description" required="required">
<br>
</div>
<div class="panel-footer">
<button type="submit" class="btn btn-success">Buscar</button>
</div>
</form>
</div>
@if (isset($noticia))
<div class="panel panel-success">
<div class="panel-heading">Resultado de busqueda</div>
<div class="panel-body">
<div class='table-responsive'>
<table class='table table-bordered table-hover'>
<thead>
<tr>
<th>ID</th>
<th>Turno</th>
<th>Fecha</th>
<th>nombre_nota</th>
</tr>
</thead>
<tbody>
@foreach($noticia as $buscar)
<tr>
<td>{{$buscar['noticiero_programa']}}</td>
<td>{{$buscar['noticiero_turno']}}</td>
<td>{{$buscar['noticiero_fecha']}}</td>
<tr/>
@endforeach
</tbody>
</table>
<center>{{ $noticia->appends(Request::only('noticiero_turno'))->links() }}</center>
</div>
</div>
<div class="panel-footer">
<a href="{{url('noticia/buscar')}}" class="btn btn-warning">Reiniciar busqueda</a>
</div>
</div>
@endif
@stop
Search engine driver
public function busqueda(Request $request)
{
$ntc_turno = $request->input('noticiero_turno');
if($ntc_turno){
$noticia = Noticia::where('noticiero_turno','LIKE',"%$ntc_turno%")
->orWhere('noticiero_programa','LIKE',"%$ntc_turno%")
->orWhere('noticiero_fecha','LIKE',"%$ntc_turno%")
->paginate(2);
return view('noticia.listar',array('noticia'=>$noticia));
}else{
$noticia = Noticia::paginate(3);
return view('noticia.listar',array('noticia'=>$noticia));
}
}