Query two different tables and show in the blade view?

2

Table 1 news Table 2 notes

The following controller makes a query to the news table. The search engine makes the query with three different fields. The question is how to make the query from the table (notes) at the same time as the news table. Then show the data in the view.

// 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));
//
//   }
//
//
// }

View

CODE OF 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($noticias))
			<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

			 <tr>

			 </tr>


				</tbody>




			</table>
<center></center>
		</div>
	</div>
		<div class="panel-footer">
		<a href="{{url('noticia/buscar')}}" class="btn btn-warning">Reiniciar busqueda</a>
		</div>
</div>
@endif

@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>
	<td>{{$buscar['nombre_nota']}}</td>

						<tr/>






       @endforeach
@endif

@stop
    
asked by CARLOS OMAR BLANCO RODRÍGUEZ 06.04.2018 в 22:37
source

1 answer

2

I assume you have a relationship created with Eloquent in this way:

Note:

class Nota extends Model
{
  ...

  public function noticia() {
    return $this->belongsTo('Noticia', 'noticia_id');
  }
}

News:

class Noticia extends Model
{
  ...

  public function notas() {
    return $this->hasMany('Nota', 'noticia_id');
  }
}

To make the query in both tables "on time", we will use whereHas (query in the relation) and orWhere:

Noticia::with('notas')
->whereHas('notas', function($query) use($ntc_turno) {
    $query->where('nombre_nota','LIKE',"%$ntc_turno%"));
})
->orWhere('noticiero_turno','LIKE',"%$ntc_turno%");
->orWhere('noticiero_programa','LIKE',"%$ntc_turno%");
->orWhere('noticiero_fecha','LIKE',"%$ntc_turno%");
->paginate(2);

More information in the Eloquent documentation:

link

link

    
answered by 06.04.2018 / 23:12
source