R
R
Rishat Sultanov2017-02-22 20:14:39
Laravel
Rishat Sultanov, 2017-02-22 20:14:39

How to correctly pass a variable from the controller to the view?

Good evening gentlemen.
I can't bring the variable from the controller to the view.
HTTP error
ERROR 500
Undefined variable: namesite (View: D:\Program\OpenServer\OpenServer\domains\univer-larave.ru\laravel\resources\views\index.blade.php)
Controller with this variable.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function index(){

    	$namesite = 'МУЦА';
    	//$descsite = 'Данная информация должна быть заменена.';


    	//return view('index')->with(['namesite'=>$namesite,'descsite'=>$descsite]);

    	return view('index')->with('namesite',$namesite);
    }	
}

index.blade.php

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rishat Sultanov, 2017-02-23
@rishatss

It was in the controller itself :)
I indicated the wrong request path there :(

A
Alexander Pushkarev, 2017-02-22
@AXP-dev

Example from my controller

public function index()
{
    $users = User::orderBy('created_at', 'desc')->paginate(30);

     return view('dashboard.pages.users.index', compact('users'));
}

Pass variables in compact. In view use{{ $users }}

F
Finsh, 2017-02-23
@Finsh

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function index(){

    	$namesite = 'МУЦА';
    	//$descsite = 'Данная информация должна быть заменена.';


    	//return view('index')->with(['namesite'=>$namesite,'descsite'=>$descsite]);

    	return view('index',['namesite' => $namesite])->with('namesite',$namesite);
    }	
}

Output in the view
{{ $namesite }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question