teampoison's avatar

variable not defined but i define variable

i create a code that show data from database but it show me variable not defined. code show data to front end

<div class="row ">
                                    @foreach ($pinned as $pin)
                                        <div class="col-6">
                                            <div class="row maan-politics-row">
                                                
                                                <div class="col-12">
                                                    <div class="maan-post-img">
                                                        @if ($pin->image)
                                                            @php
                                                                $images = json_decode($pin->image);
                                                            @endphp
                                                            @if ($images != '')
                                                                @foreach ($images as $image)
                                                                    @if (File::exists($image))
                                                                        <a
                                                                            href="@if ($pin->news_category) {{ url('') . '/' . strtolower($pin->news_category) . '/article/' . $pin->slug }} @endif">
                                                                            <img src="{{ asset($image) }}" alt="top-news">
                                                                        </a>
                                                                    @endif
                                                                @endforeach
                                                            @else
                                                                <img src="{{ asset('public/frontend/img/placeholder.jpeg') }}"
                                                                    alt="top-Blogs">
                                                            @endif
                                                        @endif

                                                        {{-- <span
                                            class="maan-tag-@if ($loop->iteration == 1) parpul @elseif($loop->iteration == 2)green @elseif($loop->iteration == 3)blue @elseif($loop->iteration == 4)red @endif">{{ $popularnews->news_category }}</span> --}}
                                                    </div>
                                                </div>
                                                <div class="col-12">
                                                    <div class="card-body maan-card-body pb-0">
                                                        <div class="maan-text">
                                                            
                                                            <h4><a
                                                                    href="@if ($pin->news_category) {{ url('') . '/' . strtolower($pin->news_category) . '/article/' . $pin->slug }} @endif">{{ $pin->title }}</a>
                                                            </h4>
                                                            <ul>
                                                                <li class=author-date>
                                                                    <span class="maan-icon"><svg viewBox="0 0 512 512">
                                                                            <path
                                                                                d="M347.216,301.211l-71.387-53.54V138.609c0-10.966-8.864-19.83-19.83-19.83c-10.966,0-19.83,8.864-19.83,19.83v118.978 c0,6.246,2.935,12.136,7.932,15.864l79.318,59.489c3.569,2.677,7.734,3.966,11.878,3.966c6.048,0,11.997-2.717,15.884-7.952 C357.766,320.208,355.981,307.775,347.216,301.211z">
                                                                            </path>
                                                                            <path
                                                                                d="M256,0C114.833,0,0,114.833,0,256s114.833,256,256,256s256-114.833,256-256S397.167,0,256,0z M256,472.341 c-119.275,0-216.341-97.066-216.341-216.341S136.725,39.659,256,39.659c119.295,0,216.341,97.066,216.341,216.341 S375.275,472.341,256,472.341z">
                                                                            </path>
                                                                        </svg></span>
                                                                    <span
                                                                        class="maan-item-text">{{ (new \Illuminate\Support\Carbon($pin->date))->format('d M, Y') }}</span>
                                                                </li>
                                                            </ul>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            
                                        </div>
                                    @endforeach

                                </div>
                            ```

create a variable to controller folder 

$pinned = News::join('newssubcategories','news.subcategory_id','=','newssubcategories.id')
            ->join('newscategories','newssubcategories.category_id','=','newscategories.id')
            ->join('users','news.reporter_id','=','users.id')
            ->select('news.id','news.title','news.summary','news.slug','news.image','news.date','news.created_at','newssubcategories.id as catid','news.meta_title','news.meta_description','newssubcategories.name as news_subcategory','newscategories.name as news_category',DB::raw("CONCAT(users.first_name,' ',users.last_name) AS reporter_name"))
            ->where('news.status',1)
            ->where('news.published_at','<=',now())
            ->Where('news.top_stories',1)
            //->where('news.speciality_id',1)
            ->orderBy('news.date', 'DESC')
            ->take(6)
            ->get();
0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error "variable not defined" usually occurs when a variable is not defined or initialized before it is used. In this case, it seems like the variable $pinned is not defined or initialized in the controller before it is being used in the view.

To fix this, you need to define and initialize the $pinned variable in the controller before passing it to the view. Here's an example:

// Define and initialize the $pinned variable
$pinned = News::join('newssubcategories','news.subcategory_id','=','newssubcategories.id')
    ->join('newscategories','newssubcategories.category_id','=','newscategories.id')
    ->join('users','news.reporter_id','=','users.id')
    ->select('news.id','news.title','news.summary','news.slug','news.image','news.date','news.created_at','newssubcategories.id as catid','news.meta_title','news.meta_description','newssubcategories.name as news_subcategory','newscategories.name as news_category',DB::raw("CONCAT(users.first_name,' ',users.last_name) AS reporter_name"))
    ->where('news.status',1)
    ->where('news.published_at','<=',now())
    ->Where('news.top_stories',1)
    //->where('news.speciality_id',1)
    ->orderBy('news.date', 'DESC')
    ->take(6)
    ->get();

// Pass the $pinned variable to the view
return view('your-view', compact('pinned'));

Make sure to replace your-view with the name of your actual view file.

Also, make sure that the controller file where you define and initialize the $pinned variable is being used by the route that renders the view.

AddWebContribution's avatar

Please return your view from controller with variable that you want to use in your blade file

for example

$websites = Website::find($name); return view('layouts/website', compact('websites'));

Please or to participate in this conversation.