2

Variables not passing to views in laravel project from the controller. getting Undefined variable: title error.

function index()
{

    $data = array(
        'title' => 'Font Awesome & Material Design Icons',
        'description' => 'Create your project with Font Awesome & Material Design Icons',
        'seo_keywords' => 'Create your project with Font Awesome & Material Design Icons',
        'data' => DB::table('fontawesomeicons')->orderBy('id', 'asc')->paginate(50),
    );   

    // $data = DB::table('fontawesomeicons')->orderBy('id', 'asc')->paginate(50);
    return view('pagination', compact('data'));
}

4 Answers 4

2

The compact function will create an array with data as the only key and the array as its value. This is not what you should do, you need an array that directly contains the variables you need in your view.

So you can just pass the data array as the second attribute for the view() function.

return view('pagination', $data);

Or if you really want to send the entire $data array as a whole variable, you have to use the array indexes in your blade views:

{{ $data['title'] }}
Sign up to request clarification or add additional context in comments.

2 Comments

I need to do it with compact because i rendering to some other blade file in pagination blade
I added an example that you can use if you want to send the entire $data array to your view.
1

Try

return view('pagination')->with(compact('data'));

Or try

$title = 'Font Awesome & Material Design Icons';
$description = 'Create your project with Font Awesome & Material Design Icons';
$seo_keywords = 'Create your project with Font Awesome & Material Design Icons';
$data = DB::table('fontawesomeicons')->orderBy('id', 'asc')->paginate(50);

return view('pagination',compac(['title','description','seo_keywords','data']));

Comments

0

compact('data') returns array like this: ['data' => $data]

So return view('pagination', compact('data')); code is equivalent to this:

return view('pagination', ['data' => $data]);

And you have access to controller variable like this: $data['title'] , $data['description'] ... You shold pass data in view instead of compact('data'):

return view('pagination', $data);

Or you can use

return view('pagination', compact('data')['data']);

But this doesn't makes sense

Comments

0

you could try something like this:

function index()
{

    $data['data'] = array(
        'title' => 'Font Awesome & Material Design Icons',
        'description' => 'Create your project with Font Awesome & Material Design Icons',
        'seo_keywords' => 'Create your project with Font Awesome & Material Design Icons',
        'data' => DB::table('fontawesomeicons')->orderBy('id', 'asc')->paginate(50),
    );   

    // $data = DB::table('fontawesomeicons')->orderBy('id', 'asc')->paginate(50);
    return view('pagination', $data);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.