php - Return a variable from a function to a view

711

I am a newbie in laravel 4 and want to return, when pressing a button, a value from a controller to the view.

My form view:

            {{ Form::submit('Save', array('class' => 'btn btn-small btn-info iframe')) }}
            <?php
            echo $test;
            ?>

My Controller:

<?php
class TestController extends BaseController {


    /**
     * Start scrapping script.
     */
    public function postTest() {
        $scrap = 'It works!';

        return View::make ( 'admin/test/index' )->with('test', $test);
    }
}

My routes:

Route::post('test', '[email protected]');

However, I get:

Undefined variable: test(View: C:\xampp\htdocs\laravel_project\lara\app\views\admin\test\index.blade.php) 

Any recommendations what I am doing wrong?

I appreciate your answer!

UPDATE

I now changed my controller like that:

public function getIndex() {

    // Show the page
    return View::make ( 'admin/test/index' );
}

public function postTest() {
    $test = 'It works!';

    return View::make ( 'admin/test/index' )->with('test', $test);
}

}

and added to my routes file:

Route::get('test', '[email protected]');
Route::post('test', '[email protected]');
Route::controller('test', 'TestController');

Furthermore, when calling:

                {{ $test}}

I getUndefined variable: $test

Any recommendations what I am doing wrong?

109

Answer

Solution:

You should change in your controller:

return View::make ( 'admin/test/index' )->with('test', $test);

to:

return View::make ( 'admin/test/index' )->with('test', $scrap);

Now you are getting undefined warning because there is no$test variable in controller.

And in Blade view, you should display it using:

{{ $test }}

or

{{{ $test }}}

(the second one for escaping characters) and not<?php echo $test; ?>

86

Answer

Solution:

change to the following:

public function postTest() {

$test = 'It works!';
return View::make ( 'admin.test.index',['test' => $test] );

}
7

Answer

Solution:

Change your make to:

return View::make ( 'admin/test/index' )->with(array('test' => $test));

Additionally, if you look at your controller, the variable you give a value to is called $scrap while the one you assign to the view is called $test, I think you mean to give them both the same name (or else it will always be empty on the view).

811

Answer

Solution:

$scrap = "Something";
return View::make ( 'admin/test/index' )->with(array('test' => $scrap)); 

Also, in your view, make sure you check that$test is actually present.

{{ Form::submit() ... }}
@if(isset($test))
{{ $test }}
@endif

OR (as per your edit) change getIndex to:

return View::make('admin/test/index')->with('test', '');

to avoid the undefined variable issue.

People are also looking for solutions to the problem: php - DB connection issues while converting mysql to mysqli

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.