Support the ongoing development of Laravel.io →
Forms Laravel.io Testing
Last updated 2 years ago.
0

Hey,

You are getting the "Array to String conversion" error because you are trying to echo an array (your $name variable in this case). The problem should not be located in the Session, but rather in your makeRandomName() method (who is apparently returning an array of objects).

To check the output of your method, add a var_dump($name) just after the call to makeRandomName()...

0

If you didn't see where the problem is located, don't hesitate to post back your makeRandomName() method...

0

Also, I'm probably missing something but I don't see where you set a session var like:

Session::put('key', 'value');

Does

$name = makeRandomName();

set a session var?

0

@jimgwhit

He passes the $name variable to the Session with the with() method when redirecting to the home route

return Redirect::route('home')->with('name', $name)
0
$name = Session::get('name');
                echo $name;

Should be un-necessary, data I pass with ->with is already ready for use, i.e.,

echo $name;

I can't find where you have to use

Session::get...

with a ->with...

0

Yeah,

I agree, when using ->with() on a view response, you can have direct access to your variables.

But in this case, @valhallathompson is using the ->with() function on a redirect. When using with on a redirect, your variables are put in the Session as flash data and yoyu can only access them by using Session::get().

Source: http://laravel.com/docs/4.2/responses#redirects (note in the red box)

0

Thanks, good to know, I just never used with a redirect.

0
Solution

Your var_dump() $name already told you that you have an array of objects.

So here comes the example.

    $a = new stdClass();
    $a->foo = 'bar';
    $b = new stdClass();
    $b->foo = 'bar2';

    Session::set('foo', array($a, $b));
    $name = Session::get('foo.0')->foo; 
    echo $name; // should print bar

In you case, to correctly print out $name, should it be this?

$name = Session::get('name.0')->expression; 
Last updated 9 years ago.
0

Hi Everyone

I apol. for my delayed response. I've marked the last response from @awsp as the answer that helped my the most. Thanks for everyone's help

Here's my function makeRandomName();

	function makeRandomName(){ 
		/*
		SELECT expression
		FROM sayings
		ORDER BY RAND()
		LIMIT 1
		*/
		$string = DB::table('sayings')
		    ->select('expression')
                    ->orderBy(DB::raw('RAND()'))
                    ->take(1) // take means how many do you want? AKA limit
                    ->get();
		return $string;
	}

On my homepage I've also added this syntax as well using the isset php function

		@if(isset(Session::get('name.0')->expression))
			{{ Session::get('name.0')->expression }}
		@endif
Last updated 9 years ago.
0

Just last little trick to make it cleaner

If the purpose of your function was to return just one string...

function makeRandomName(){ 

        $string = DB::table('sayings')
            ->select('expression')
                    ->orderBy(DB::raw('RAND()'))
                    ->first();  // More appropriate when returning just one row, first returns directly an object and not an array of objects

        return $string->expression;
}

Session::put('name', randomName() );

Session::get('name'); // Will directly return the name as a string

Happy coding...

0

Thanks Saluki, it's working great

		@if(isset(Session::get('name')->expression))
			{{ Session::get('name')->expression }}
		@endif
	function makeRandomPetName(){
		$string = DB::table('sayings')
					->select('expression')
                    ->orderBy(DB::raw('RAND()'))
                    ->take(1) // take means how many do you want? AKA limit
                    /*->get();*/
                    ->first();
		/*return $string->expression;*/
		return $string;
	}
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.