#Question about showing database entries on blade view

23 messages · Page 1 of 1 (latest)

trail heath
#

Hi everyone,
I am creating a sort of "marketplace" website where people can shop for items like wine,
I am unsure of the best way to acquire all of the entries in my database to put them on a blade view.
with the current design, the list of products will show on my home blade view (the root view)
is it considered good practice to call my controller from my blade view to fetch all the items? is there a better way to proceed? (I saw that you could redirect to controllers that themselves would redirect to views, but I'm not sure if that fits my usecase, in the sense, is it possible to pass a possibly big array to a view from a controller)

still dock
#

I’m assuming your wine product details will be stored in a database…

And I’m assuming you are interacting with it using models?

Normal practice is for the route to point to a method inside a controller, ie: index() which loads the view (blade file)

Inside the method you would call the database and return all the wine items in a variable as a collection and then return the view inside the method and pass the variable along with it to the blade view.

So yes, it’s possible to pass a big array from a controller to a view, it’s considered a normal thing to do

#

@trail heath

trail heath
#

ok cool, I was just wondering, since it's the main view, I didn't know if it worked differently or not

#

will it still work if I extensively use extends in the view I redirect to ? (like so):

@extends('layouts.layout')

@extends('layouts.navigation')
#

I plan to do the part where the wines are showed in another layout not yet created

still dock
trail heath
#

I don't understand how to use blade properly, I always end up with circular include/extends

#

not the good topic

trail heath
#

the catalog onto which the products show up is not a view of its own, it is supposed to be showing on the main view

#

but I really don't understand how to use sections and extends

#

say, this is my main view :

@extends('layouts.app')

@include('navigation')
@include('catalogue')

@section('content')
    @yield('navbar')
    @yield('catalog.content')
@endsection

layouts.app is the following :

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>WineDictionary</title>

    <!-- Fonts
    <link rel="preconnect" href="https://fonts.bunny.net">
    <link href="https://fonts.bunny.net/css?family=figtree:400,600&display=swap" rel="stylesheet" />
     -->
    <link href="{{ Vite::asset('resources/css/app.css') }}" rel="stylesheet">

    @yield("styles")

    @yield("scripts")
</head>
<body>
@yield('catalog.content')
</body>
</html>
#

and the catalog layout is the following :

@extends('home')

@section('catalog.content')
    <div class="grid gap-4 grid-cols-3 grid-rows-3">
        @foreach($wines as $wine)
            {{ $wine->name }}
            {{ $wine->property_name }}
            {{ $wine->appellation }}
            {{ $wine->vintage }}
            {{ $wine->image_url }}
        @endforeach
    </div>
@endsection
#

(the runtime can't even run that, since it runs into itself)

#

I see what I've done wrong, but I don't know how to make it right

#

if I remove the extends from the catalog, then how will I be to yield the catalog.content ?

still dock
#

Usually you would have a main layout which has all the head information and meta data and pulls in any scripts or css files and you would use @yield(‘content’). Then in another blade view you would extend the main layout and then call @section(‘content’) and then put anything between that and the closing @section. @trail heath

Which laravel version are you using? And have you considered a starter kit like breeze?

trail heath
#

I use laravel 10.0

#

I use laravel breeze but I still have to use blade

still dock
trail heath
#

I am working on it right now

#

ok I've done that, but the content in my main layout file is not showing 🤔