I have a simple phone book application with a main view, index.blade.php that includes a partial view, contact_list.blade.php.
I only have a single route that refers to my controller, ContactListController.php.
From inside contact_list.blade.php I need access to three methods of my controller.
One of the methods returns the data I need to pass as parameter for the other two.
Here's what my application looks like so far:
I want to be able to type a name in the search form and then filter the names of usuarios in the contact_list view.
Here's search_contact.blade.php, where the search form is located:
<div class="col-lg-4 inp">
<form action="." method="GET">
<div class="form-group">
<input type="text" name="searchName" id="searchName" class="form-control mt-2 mb-3" placeholder="search">
<input class="btn btn-info w-100 btn1" type="submit" value="search">
</div>
</form>
</div>
Here's contact_list.blade.php:
<div class="col-lg-8">
<table id="myTable" class="table text-justify table-striped">
<thead class="tableh1">
<th class="">Name</th>
<th class="col-3">NÂș of Phones</th>
<th class="">Phones</th>
</thead>
<tbody id="tableBody">
@if (!isset($_GET["searchName"]))
@foreach ($data as $item)
<tr>
<!-- Test -->
<td>{{ $item->nome }}</td>
<td>{{ $item->numero_count }}</td>
<td>
<ul style="list-style: none;">
<?php $phones = app\Http\Controllers\ContactListController::get_telefones_by_usuario($item->u_id) ?>
@foreach ($phones as $phone)
<li><i class="bi bi-telephone-fill pe-2" style="color: #0d6efd"></i>{{ $phone->numero }}</li>
@endforeach
</ul>
</td>
</tr>
@endforeach
@else
<?php $my_data = app\Http\Controllers\ContactListController::get_users_by_name(request()) ?>
@foreach ($my_data as $item)
<tr>
<!-- Test -->
<td>{{ $item->nome }}</td>
<td>{{ $item->numero_count }}</td>
<td>
<ul style="list-style: none;">
<?php $phones = app\Http\Controllers\ContactListController::get_telefones_by_usuario($item->u_id) ?>
@foreach ($phones as $phone)
<li><i class="bi bi-telephone-fill pe-2" style="color: #0d6efd"></i>{{ $phone->numero }}</li>
@endforeach
</ul>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
</div>
Here are my three methods inside my controller:
public function index()
{
// $usuarios = Usuario::all();
// $telefones = Telefone::all();
$data = Usuario::join("telefones", "usuarios.id", "=", "telefones.id_usuario")
->select(
"usuarios.id as u_id",
"usuarios.nome as nome",
"telefones.numero as numero",
DB::raw("COUNT(telefones.numero) AS numero_count")
)
->groupBy("usuarios.nome")
->orderBy("usuarios.nome")
->get();
return view("index", compact("data"));
}
public static function get_telefones_by_usuario($id)
{
$telefones = Telefone::join("usuarios", "usuarios.id", "=", "telefones.id_usuario")
->select("telefones.numero as numero")
->where("usuarios.id", "=", $id)
->get();
return $telefones;
// return view("index", compact("telefones"));
}
public static function get_users_by_name(Request $request)
{
$name = $request->input("searchName");
$usuarios = Usuario::join("telefones", "usuarios.id", "=", "telefones.id_usuario")
->select(
"usuarios.id as u_id",
"usuarios.nome as nome",
"telefones.numero as numero",
DB::raw("COUNT(telefones.numero) AS numero_count")
)
->where("usuarios.nome", "LIKE", "$" . $name . "%")
->groupBy("usuarios.nome")
->orderBy("usuarios.nome")
->get();
return $usuarios;
}
I call my controller method from inside the partial view, but I heard it's bad practice to do that. If that's the case, then how can I achieve what I want?
My idea was to access the GET parameter searchName
(from the form inside search_contact.blade.php partial view) from within my contact_list view, by calling the controller method. But I'm getting zero results back with my code as it is:
How can I achieve what I want? What am I doing wrong?
Is it really that bad to call a controller method from inside a view? If so, what can I do instead?
Thank you.