How to pass method objects that are in different controllers in Laravel?

Asked

Viewed 37 times

0

Good night.

What I need is to display the name of the book where your id is (in the Form Book column). inserir a descrição da imagem aqui And so that I don’t do it in a way outside the convention, I’d like to know the right way to do it.

I have the method dashboard that’s in the controller Homecontroller which passes to my view the objects below to form the table:

    public function painel(){
        $livro = Livro::all();
        $lote = Lote::all();
        $status = Status::all();
        return view('painel.index', compact('livro', 'lote', 'status'));
        
    }

And I also have the controller Lotecontroller where from there I would need to pass to that same view 'index panel.' the book name (book->title) and not the book id that is in the Lot table (lot->book_id).

I would like to know the correct way to do such a procedure. If I’m wrong in the way I’m doing, I’m free to take criticism to improve my code. Thank you!

View panel.index:

div class="container">
    <h1>Consulta de Lotes</h1>
    <table class="table">
        <thead class="thead-dark">
            <tr>
                <th>Lote</th>
                <th>Livro</th>
                <th>Data Início</th>
                <th>Data de Encerramento</th>
            </tr>
        </thead>
        <tbody>
            

            @foreach ($lote as $lotes)
                <tr>
                    <th>{{ $lotes->id }}</th>
                    <th>{{ $lotes->livro_id }}</th>
                    <th>{{ $lotes->dia_inicio }}</th>
                    <th>{{ $lotes->dia_fim }}</th>

                </tr>
            @endforeach
        </tbody>
    </table>
</div>

2 answers

0

If in your book Migration you created the book_id as foreign key referencing the books table, just vc create the relationship in the batch template and use the relationship anywhere in the system you need.

In the Migration of the batch table:

$table->unisignedInteger('livro_id');
$table->foregin('livro_id')->references('id')->on('livros')->onDelete('cascade');

Check your books ID was created with Integer or Biginteger, the foreign key has to be the same type only unsigned

In the Batch Model

public function livro() {
   return $this->hasOne(Livro::class);
}

Then, in the controller you need you make the query:

$lotes = Lotes::with(['livro'])->get(); // o with aqui é opcional, vai evitar que na view o laravel faça outra consulta no banco
return view('painel.index', ['lotes' => $lotes]);

In view

@foreach ($lotes as $lote)
<tr>
   <td>{{ $lote->id }}</td>
   <td>{{ $lote->livro->titulo }}</td> // se este for o compo da tabela livros que vc quer
   <td>{{ $lotes->dia_inicio }}</td>
   <td>{{ $lotes->dia_fim }}</td>
</tr>
@endforeach

I wrote the code here and now, there may be some mistake, but that’s the idea...

You can still in the book template have a method that returns all the batches using hasMany (think that’s it)

-1

You can make a Join in the query, example:

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();
  • Thanks friend. I did so right there while waiting for the answers. But anyway I need to separate each thing in its model (class), to make coherent.

Browser other questions tagged

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