Search engine that queries two different tables laravel 5.4

1

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.

Tables

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 graph

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

  }
}
    
asked by CARLOS OMAR BLANCO RODRÍGUEZ 25.03.2018 в 01:17
source

1 answer

1

When you have a defined relationship you can use with to bring the data of the relationship, in the following way:

$noticia = Noticia::with('notas')->get();// notas es el nombre de la relación de que tienes en tu modelo Noticias.

But there are times when we need to consult the table of the relationship.

Then we can use with as an array.

$data='2:30';
$noticia = Noticia::with(['notas'=>function($query)use($data){
    $query->where('nombre_nota',"Una nota")->where('duracion_nota',$data);
}])->get();

The% part of% co is optional, only if you need external data.

Now the access to the data will be as follows:

foreach($noticias as $noti){
    echo $noti->noticiero_programa;
    foreach($noti->notas as $notas){
        echo $noti->nombre_nota;
    }
}
    
answered by 25.03.2018 / 01:40
source