Call foreach result Controller checkbox and show in view

Asked

Viewed 825 times

2

I need to recover a method in the controller and display in the view,

Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Categoria;
use App\Models\Sessoes;
class todosProdutosController extends Controller{
public function categorias(){

    $categorias = Categoria::where('nome', 'LIKE', '%a%')->get();
    foreach ($categorias as $categoria) {
        $categoriaNome = $categoria->nome;
        $sessao = $categoria->sessoes; // posso adicionar condições
        foreach ($sessao as $sessao_) {
            $sessao_->nome;
        }
return view('home.home', 'produto', $categoriaNome);
    }
}}

And in my view it’s like this

<section id="produtos">
<div class="container">
    <div class="row">
        <div class="col-md-12">

            {{ dd($categoriaNome) }}
        </div>
    </div>
</div>

Some help?

1 answer

1


Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Categoria;
use App\Models\Sessoes;
class todosProdutosController extends Controller{
public function categorias(){

    $categorias = Categoria::where('nome', 'LIKE', '%a%')->get();
    return view('home.home', compact('categorias'));
}}

And your view stays like this:

<section id="produtos">
<div class="container">
    <div class="row">
        <div class="col-md-12">
            @foreach($categorias as $categoria)
                {{$categoria->nome}}
            @endforeach
        </div>
    </div>
</div>
  • In the tags foreach you don’t need to add keys "{{"

  • @Luigi Jordânio keeps returning me error >> Undefined variable: categories (View: F: xampp htdocs study Resources views home home.blade.php) where I may be missing?

  • 1

    I found the problem, is that he had an extra argument, thus: Return view('home.home', Compact('categories')); Thank you very much for your help =)

Browser other questions tagged

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