Undefined Variable in partial view

Published 1 year ago by kadnan

I am using Larvel 5.3.

I am passing variable in my view partial but in partial it says variable not found. I am doing following.

@include('partials._latest',['title' => 'Latest Products','entries' => $entries])

and in partial I am calling as {{$title}}

I am getting:

ErrorException in 4ffcd30ab3dcb337e74cecc7f61f71723f113d8f.php line 3:
Undefined variable: title (View:..

Please note that title is not being passed from Controller to parent View. It was only visible for partial view.

update

Code Repo is here: https://github.com/kadnan/simpleb2b

Best Answer (As Selected By kadnan)
SaeedPrez

@kadnan You have two includes, one without passing the variables to it..

https://github.com/kadnan/simpleb2b/blob/master/resources/views/home.blade.php#L29

This was something that was missing from the code you originally provided:

<div class="row large-margin-top hidden-xs">
                   
                    <div style="margin-left: -20px;" class="col-md-5">
                        @include('partials._latest',['title' => "Latest Products"])
                    </div>
                </div>

And the reason why I asked to see the whole thing. It has happened so many times before that people provide partial code (where they assume the error is) and we spend a lot of time trying to help and then they find a silly mistake somewhere else in the code..

SaeedPrez
SaeedPrez
1 year ago (263,260 XP)

Hm..

See if php artisan view:clear helps..

kadnan

It does not.

SaeedPrez
SaeedPrez
1 year ago (263,260 XP)

I don't see why the code you've shown should not work.

If you want, show your whole template and also the partial so we can take a closer look.

kadnan

There is nothing complicated in it. I paste the code that is right there. Nothing fancy. If you have Laravel 5.3.15 you can give it a try.

I also called get_defined_vars() and it does not return the variable in return.

SaeedPrez
SaeedPrez
1 year ago (263,260 XP)

I'm actually currently working on a project with 5.3.15 and I have no problems with passing data to partials.

kadnan

Partial Code

{{--Latest Box--}}

<ul class="list-group">
    <li  class="list-group-item" style="background-color: #222222;color: white;">{!! $title !!}</li>
    <li  class="list-group-item" ><a title="Agriculture" href="#">Agriculture</a></li>
    <li  class="list-group-item" ><a title="Clothing" href="#">Clothing</a></li>
    <li  class="list-group-item" ><a title="Clothing" href="#">Clothing</a></li>
    <li  class="list-group-item" ><a title="Clothing" href="#">Clothing</a></li>
    <li  class="list-group-item" ><a title="Clothing" href="#">Clothing</a></li>
</ul>

home.blade.php

@extends('layouts.master')
@section('content')
<div class="row large-margin-top hidden-xs">
                    <div class="col-md-5 col-md-offset-1">
                        @include('partials._latest')
                    </div>
                    <div style="margin-left: -20px;" class="col-md-5">
                        @include('partials._latest',['title' => "Latest Product"])
                    </div>
                </div>
SaeedPrez
SaeedPrez
1 year ago (263,260 XP)

You're making it pretty impossible to help you by hiding your code. What you've provided is what you assume should house the error and it doesn't even include the use of $title in the partial.

Unless you're sending $title and then using $title_latest_product in your partial, I can't see more and therefore I can't help you.

Anyways, good luck.

kadnan

@SaeedPrez That was a mistake while I was trying out different things and got pasted from clipboard. I just made changes

kadnan
SaeedPrez
SaeedPrez
1 year ago (263,260 XP)

@kadnan You have two includes, one without passing the variables to it..

https://github.com/kadnan/simpleb2b/blob/master/resources/views/home.blade.php#L29

This was something that was missing from the code you originally provided:

<div class="row large-margin-top hidden-xs">
                   
                    <div style="margin-left: -20px;" class="col-md-5">
                        @include('partials._latest',['title' => "Latest Products"])
                    </div>
                </div>

And the reason why I asked to see the whole thing. It has happened so many times before that people provide partial code (where they assume the error is) and we spend a lot of time trying to help and then they find a silly mistake somewhere else in the code..

kadnan

@SaeedPrez God!!! It was so obvious which Ignored. Sorry I should have understood earlier

SaeedPrez
SaeedPrez
1 year ago (263,260 XP)

@kadnan don't worry about it, happens to all of us but try provide more code next time ☺

Also don't forget to post the solution in your GitHub issue as well so people who have similar problem can find the solution. And mark as solved here on Laracasts as well.

kadnan

Yep, just did that. Thanks again. I had no issue to provide code, it just I did not think of it. anyways, have a nice weekend! :)

jonjie

It helps me analyzed my code haha

Please sign in or create an account to participate in this conversation.