Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

f.alabakhsh@gmail.com's avatar

I want to pass {{posts}} to welcome component, how can i do that?

I want to pass {{posts}} to admin dashboard, how can i do that?

i get this erorr {{{ Undefined variable $posts }}}

<?php

namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Post;
use Illuminate\Http\Request;
use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;
use Illuminate\Support\Facades\Gate;
use Symfony\Component\HttpFoundation\Response;

class AdminController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $users = User::with('roles')->get();
        $posts = Post::with('category')->latest()->paginate(10);
        return view('admin.index',['users' ,'posts']);
    }
        public function create()
    {
        abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $roles = Role::pluck('title', 'id');

        return view('users.create', compact('roles'));
    }

    public function store(StoreUserRequest $request)
    {
        $user = User::create($request->validated());
        $user->roles()->sync($request->input('roles', []));

        return redirect()->route('users.index');
    }

    public function show(User $user)
    {
        abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return view('users.show', compact('user'));
    }

    public function edit(User $user)
    {
        abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $roles = Role::pluck('title', 'id');

        $user->load('roles');

        return view('users.edit', compact('user', 'roles'));
    }

    public function update(UpdateUserRequest $request, User $user)
    {
        $user->update($request->validated());
        $user->roles()->sync($request->input('roles', []));

        return redirect()->route('users.index');
    }

    public function destroy(User $user)
    {
        abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $user->delete();

        return redirect()->route('users.index');
    }

}

and this is app.blade.php


<body class="font-sans antialiased overflow-x-hidden bg-gray-100 dark:bg-gray-900 dark:bg-opacity-40">
        <x-jet-banner />
        <div class="w-full h-full">
            <div class="flex flex-no-wrap">
                <!-- Sidebar starts -->
                @livewire('navigation-menu')
                <div class="w-full px-6 md:pl-[2.3rem] md:pr-[18.5rem] sm:pl-[2.3rem] sm:pr-[18.5rem] mt-10">
                    <!-- Page Heading -->
                    @if (isset($header))
                        <header class="flex-shrink max-w-full px-4 w-full">
                            {{ $header }}
                        </header>
                    @endif
                    <!-- Page main -->
                    <main  class="w-full rounded my-10">
                        @yield('content')
                    </main>
                </div>
            </div>
        </div>
        @stack('modals')

        @livewireScripts
        <script>
            var sideBar = document.getElementById("mobile-nav");
            var openSidebar = document.getElementById("openSideBar");
            var closeSidebar = document.getElementById("closeSideBar");
            sideBar.style.transform = "translateX(256px)";

            function sidebarHandler(flag) {
                if (flag) {
                    sideBar.style.transform = "translateX(0px)";
                    openSidebar.classList.add("hidden");
                    closeSidebar.classList.remove("hidden");
                } else {
                    sideBar.style.transform = "translateX(256px)";
                    closeSidebar.classList.add("hidden");
                    openSidebar.classList.remove("hidden");
                }
            }
        </script>
    </body>

this is dashboard.blade.php


@extends('layouts.app')

@section('content')

<x-slot name="header">
    <!-- breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="flex flex-wrap justify-start bg-transparent text-sm mb-0 space-x-1">
            <li>
            <a href="#" class="hover:text-indigo-500 font-semibold text-xl text-gray-800 leading-tight">
                {{ __('دشبورد') }}
            </a>
            </li>
        </ol>
    </nav>
</x-slot>
<x-jet-welcome />
@stop

and i want show post in this compnent welcome.blade.php

<div class="hidden-header hidden-sort-after">
                <div class="dataTable-wrapper dataTable-loading no-footer sortable searchable fixed-columns">
                    <div class="dataTable-top">
                        <div class="dataTable-dropdown">
                            <label>
                                <select class="dataTable-selector">
                                    <option value="5">5</option>
                                    <option value="10" selected="">10</option>
                                    <option value="15">15</option>
                                    <option value="20">20</option>
                                    <option value="25">25</option>
                                </select>
                                entries per page
                            </label>
                        </div>
                        <div class="dataTable-search">
                            <input class="dataTable-input" placeholder="Search..." type="text"></div>
                        </div>
                        <div class="dataTable-container">
                            <table class="table-sorter table-bordered-bottom w-full text-sm ltr:text-left rtl:text-right text-gray-500 dark:text-gray-400 dataTable-table">
                                <thead>
                                    <tr class="bg-gray-100 dark:bg-gray-900 dark:bg-opacity-40">
                                        @foreach ($posts as $post)
                                        <th data-sortable="" style="width: 46.9534%;">
                                            <a href="#" class="dataTable-sorter">
                                                {{ $post->title }}
                                            </a>
                                        </th>
                                        @endforeach
                                        <th data-sortable="" style="width: 17.5627%;">
                                            <a href="#" class="dataTable-sorter">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-eye" viewBox="0 0 16 16">
                                                <title>بازدیدها</title>
                                                <path d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.133 13.133 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.133 13.133 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5c-2.12 0-3.879-1.168-5.168-2.457A13.134 13.134 0 0 1 1.172 8z"></path>
                                                <path d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5zM4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0z"></path>
                                            </svg>
                                            </a>
                                        </th>
                                        <th data-sortable="" style="width: 17.5627%;">
                                            <a href="#" class="dataTable-sorter">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-hand-thumbs-up" viewBox="0 0 16 16">
                                                    <title>لایک ها</title>
                                                    <path d="M8.864.046C7.908-.193 7.02.53 6.956 1.466c-.072 1.051-.23 2.016-.428 2.59-.125.36-.479 1.013-1.04 1.639-.557.623-1.282 1.178-2.131 1.41C2.685 7.288 2 7.87 2 8.72v4.001c0 .845.682 1.464 1.448 1.545 1.07.114 1.564.415 2.068.723l.048.03c.272.165.578.348.97.484.397.136.861.217 1.466.217h3.5c.937 0 1.599-.477 1.934-1.064a1.86 1.86 0 0 0 .254-.912c0-.152-.023-.312-.077-.464.201-.263.38-.578.488-.901.11-.33.172-.762.004-1.149.069-.13.12-.269.159-.403.077-.27.113-.568.113-.857 0-.288-.036-.585-.113-.856a2.144 2.144 0 0 0-.138-.362 1.9 1.9 0 0 0 .234-1.734c-.206-.592-.682-1.1-1.2-1.272-.847-.282-1.803-.276-2.516-.211a9.84 9.84 0 0 0-.443.05 9.365 9.365 0 0 0-.062-4.509A1.38 1.38 0 0 0 9.125.111L8.864.046zM11.5 14.721H8c-.51 0-.863-.069-1.14-.164-.281-.097-.506-.228-.776-.393l-.04-.024c-.555-.339-1.198-.731-2.49-.868-.333-.036-.554-.29-.554-.55V8.72c0-.254.226-.543.62-.65 1.095-.3 1.977-.996 2.614-1.708.635-.71 1.064-1.475 1.238-1.978.243-.7.407-1.768.482-2.85.025-.362.36-.594.667-.518l.262.066c.16.04.258.143.288.255a8.34 8.34 0 0 1-.145 4.725.5.5 0 0 0 .595.644l.003-.001.014-.003.058-.014a8.908 8.908 0 0 1 1.036-.157c.663-.06 1.457-.054 2.11.164.175.058.45.3.57.65.107.308.087.67-.266 1.022l-.353.353.353.354c.043.043.105.141.154.315.048.167.075.37.075.581 0 .212-.027.414-.075.582-.05.174-.111.272-.154.315l-.353.353.353.354c.047.047.109.177.005.488a2.224 2.224 0 0 1-.505.805l-.353.353.353.354c.006.005.041.05.041.17a.866.866 0 0 1-.121.416c-.165.288-.503.56-1.066.56z"></path>
                                                </svg>
                                            </a>
                                        </th>
                                        <th data-sortable="" style="width: 17.9211%;">
                                            <a href="#" class="dataTable-sorter">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chat-left-text" viewBox="0 0 16 16">
                                                    <title>کامنت ها</title>
                                                    <path d="M14 1a1 1 0 0 1 1 1v8a1 1 0 0 1-1 1H4.414A2 2 0 0 0 3 11.586l-2 2V2a1 1 0 0 1 1-1h12zM2 0a2 2 0 0 0-2 2v12.793a.5.5 0 0 0 .854.353l2.853-2.853A1 1 0 0 1 4.414 12H14a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2z"></path>
                                                    <path d="M3 3.5a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5zM3 6a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9A.5.5 0 0 1 3 6zm0 2.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5z"></path>
                                                </svg>
                                            </a>
                                        </th>
                                    </tr>
                                </thead>

                                <tbody>

                                </tbody>
                            </table>
                        </div>
                        <div class="dataTable-bottom">
                            <div class="dataTable-info">
                                نمایش 1 تا 10 از 48 اطلاعات
                            </div>
                            <nav class="dataTable-pagination">
                                <ul class="dataTable-pagination-list">
                                    <li class="active">
                                        <a href="#" data-page="1">1</a>
                                    </li>
                                    <li class="">
                                        <a href="#" data-page="2">2</a>
                                    </li>
                                    <li class="pager">
                                        <a href="#" data-page="2">›</a>
                                    </li>
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>

0 likes
5 replies
tykus's avatar

You return the admin.index view; how is this relevant to app, dashboard and welcome templates?

tykus's avatar

@f.alabakhsh@gmail.com I still have no idea how all of these view are used with each other; all I see is the admin.index view template gets $posts passed to it, nothing else.

nikulpaladiya's avatar

@f.alabakhsh I don't think it will work. Because your welcome.blade.php file didn't connect with app.blade.php or dashboard.blade.php file

CosminComan's avatar

I will leave some useful links for you to check out, so you can figure it out yourself:

How to pass parameters from Laravel to Blade component: https://laravel.com/docs/10.x/blade#introduction

Assuming your plural naming, posts will be an array/collection so you have to iterate it. In blade you can't display an array/collection directly, so you do this: https://laravel.com/docs/10.x/blade#loops

And I think this is all you need for your issue!

Please or to participate in this conversation.