How do I pass an array of data to a readable view?

Asked

Viewed 1,475 times

1

How I pass a result from a select to a view?

public function index()
{
    $equipams = DB::table('equipamentos')
            ->select('equipamentos.*', 'tipoequipamentos.descricaotipoequip', 'locals.descricaolocal',
                     'tipoequipamentos.descricaotipoequip', 'fabricantes.descricaofabricante',
                     'fornecedors.descricaofornecedor')
            ->join('tipoequipamentos', 'tipoequipamentos.id','=', 'equipamentos.idtipoequipamento')
            ->join('fabricantes', 'fabricantes.id','=', 'equipamentos.idfabr')
            ->join('fornecedors', 'fornecedors.id','=', 'equipamentos.idforn')
            ->join('locals', 'locals.id','=', 'equipamentos.idloca')
            ->get();

    //dd($equipam);
    //$equipamentos = \App\Models\Portal\Equipamento::paginate(3);
    return view('portal.equipamento.index', 'equipam');
}
  • In view: @forelse ($equipams as) Date error: syntax error, Unexpected ')', expecting '('

  • Would not be @foreach($equipams as $equipam)?

1 answer

1

Thus

return view('portal.equipamento.index', compact('equipams'));

Or so:

return view('portal.equipamento.index', ['equipams'=>$equipams]);

No view can reference like this $equipams.

I recommend reading the documentation or a tutorial, because this is a very basic question.

  • It worked perfectly. One thing I find funny that the links() function does not work with selects arrays? Only with tablets?

  • If I pass as array gives error. If I pass as object gives another error: Call to Undefined method stdClass:links()

  • That’s why you’re using ->get(), should instead use ->paginate(15), for pages of 15 elements for example. More details: https://laravel.com/docs/5.3/pagination. Ah, and don’t forget to mark this answer with correct, as it solved your problem.

Browser other questions tagged

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