List data from two unrelated tables in the same view

Asked

Viewed 188 times

0

I have a view called form where I need it to appear, in the select field, the States(UF) coming from Mysql. I have the EstadoController where in the index method I do the listing using all. Now, I want this listing to appear in the form view whose route is: Route::get('/formulario', 'FormularioController@create');

To EstadoController is like this:

public function index(Estado $estado){       
    $retornoAllEstado = $estado->all();
    return view('painel-adm.formulario', compact('retornoAllEstado'));
}

But when I foreach in the view form, I come across error:

<select class="custom-select" name="qpae_uf_rg">              
    @foreach($retornoAllEstado as $estado)
        <option value="1">{{$estado->uf}}</option>
    @endforeach          
</select>   

Error generated: Undefined variable: retornoAllEstado...

Formulariocontroller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

/* Usando a model Formulario */
use App\Models\Painel\Formulario;

class FormularioController extends Controller
{
    private $formulario;


    public function __construct(Formulario $formulario)
    {

        $this->formulario = $formulario;
    }

    public function index()
    {

    }


    public function create()
    {
        return view('painel-adm.formulario');
    }  

    public function store(Request $request)
    {       


    }

    public function show($id)
    {

    }

    public function edit($id)
    {

    }

    public function update(Request $request, $id)
    {

    }

    public function destroy($id)
    {

    }
}

formulario.blade.php

<div class="form-group col-md-1">
        <label>UF</label>

        <select class="custom-select" name="uf">              
            @foreach($retornoAllEstado as $estado)
                <option value="1">{{$estado->uf}}</option>
            @endforeach          
        </select>           

    </div>
  • executes a dd($retornoAllEstado);after the line you get all the records, and tell me the result.

  • Well, returned an array with the 27 items, but for me to see the result without error, I had to create a route Route::get('/states-list', 'Estadocontroller@index'); But now you need these UF listings to appear in my form view.

  • Refreshes the question and puts the controller that returns from that form and the view. .

  • I don’t know if I was very clear. When I type in the browser /states-list opens the.php status view and correctly appears the UF listing. Now, I need this listing to appear in the view formulario.blade.php

  • Run a test on the answer I gave and see if that’s it.

2 answers

1

puts a dd(&returns) likely that you see it empty, that’s because your all() is running on the $status variable. which is not normal on the index

suggestion:

$retornoAllEstado = Estado::all(); 
  • It also worked, but I had to create a route Route::get('/states-list', 'Estadocontroller@index'); Now I need this listing to appear in the view form Route::get('/formulario', 'Formulariocontroller@create');

0


Change the method create():

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Estado;
/**
* 
* code..
*/
public function create()
{
    $retornoAllEstado = new Estado::all();
    return view('painel-adm.formulario', compact('retornoAllEstado'));
} 
/**
* 
* code..
*/

remember to call the model at the beginning of your class use App/Estado;

Test and see if it works as you need it.

  • Ok, it worked out! However, a doubt. I made this change in Formulariocontroller, but Estadocontroller I’m not using it for anything. I can do all in Statentroller and then display the listing in the view form Route::get('/form', 'Formulariocontroller@create'); ???

  • @Barraviera did not understand your question, but this EstadoController the most you could use would be for management, I think you’re confusing the controller with the model. but explain to me what you want so I can try to help you.

  • I have the route that shows the Route::get('/form', 'Formulariocontroller@create'); so I would like when I type /form the state listing to appear in the form select. This is already working, but I’m pulling this listing with all in Formulariocontroller and not in Statentroller. I’m starting on the Rover, maybe I’m confusing something. So it seems to me that I don’t need to have a specific controller for each view?

  • 1

    @Barraviera took a look at these posts I did a few years ago Laravel - Bulfaitelo, I think you’re confusing the controller with the model, Laravel "works" with MVC, model (basically the data) view (the visual return to the client) Controller (basically the logical part). the fact that you have a State controller does not mean that it has to be used, as it is only a controller.

  • Great article! Will help me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.