Asked  7 Months ago    Answers:  5   Viewed   38 times

I have been having trouble storing an array in session. I am making a shopping cart and it doesn't seem to work.

public function __construct(){

  $product = array(1,2,3,4);
  Session::push('cart', $product);

}

and then retrieve it in the view like this.

{{Session::get('cart')}}

However I keep getting an error like this.

htmlentities() expects parameter 1 to be string, array given

Any clues and advice on how to create a shopping cart that stores an array of items.

 Answers

90

If you need to use the array from session as a string, you need to use Collection like this:

$product = collect([1,2,3,4]);
Session::push('cart', $product);

This will make it work when you will be using {{Session::get('cart');}} in your htmls. Be aware of Session::push because it will append always the new products in sessions. You should be using Session::put to be sure the products will be always updating.

Wednesday, March 31, 2021
 
Rocket
answered 7 Months ago
58

Looks like a bug at Laravel's side:

https://github.com/laravel/installer/blob/master/src/NewCommand.php#L125

Trying to download in the browser:

http://cabinet.laravel.com/latest-52.zip

results in a 404.

Wednesday, March 31, 2021
 
Jesse
answered 7 Months ago
18

You need to set value properly.

            $movies_id = array();

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $id = $row['id'];
                $title_id = $row['title_id'];
                    $movies_id[$id] = $title_id; // The proper way
                    //$movies_id[] = [$id => $title_id];
            }

            print_r($movies_id);

The more readable code would be:

            $movies_id = array();

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $movies_id[$row['id']] = $row['title_id'];
            }
            print_r($movies_id);

And would result in

array (
    1 => 'Title 1',
    2 => 'Title 2',
    4 => 'Title 4',
    8 => 'Title 8',
)

And key-less soulution would be:

            $movies_id = array();

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $movies_id[] = $row['title_id'];
            }
            print_r($movies_id);

And would result in

array (
    0 => 'Title 1',
    1 => 'Title 2',
    2 => 'Title 4',
    3 => 'Title 8',
)
Friday, May 28, 2021
 
simPod
answered 5 Months ago
12

When you're running the function a() in your eval'ed string, the variable $ar doesn't exist anymore. That's triggering an error, which causes the eval() to fail.

Since you're using eval(), a quick-and-dirty hacky way to fix it seems appropriate. ;-)

Instead of doing this:

$run = " a('".$str."', $ar); ";

You can do this:

$run = " a('$str', ". var_export($ar, true) ."); ";

This will cause the string $run to look like this if you were to echo it:

a('something', array(
  'a' => 'value1',
  'b' => 'value2',
));

So now you're passing the array directly into the function call, instead of passing a variable.

Saturday, May 29, 2021
 
edorian
answered 5 Months ago
29

It seems like your are not attaching the replies to your $post variable while passing it to your view.
We have to use with() method to join these two tables.
Try, the following :

if(Auth::check()){ 
            $posts = Post::notReply()
                    ->where(function($query){ 
                        return $query->where('user_id', Auth::user()->id)
                                ->orWhereIn('user_id', Auth::user()->friends()->lists('id'));
                    })
                    ->with('replies')//Here we are joining the replies to the post
                    ->orderBy('created_at', 'desc')
                    ->get();
                    return view('timeline.index', compact('posts')); 
                }

Now, in your View you would need to use 2 foreach loop like below:

@foreach($posts as $post)
    {{--Your Code --}}
    @foreach($post->replies as $reply)
        {{--Your Code --}}
    @endforeach
    {{--Your Code --}}
@endforeach

Update:

Problem is with the postReply() method. In old code you were overriding the $post variable, due to which the reply was getting parent_id as its own id.
So, replace your old code with using $reply variable.
old:

$post= Post::create([
   'body' => $request->input("reply-{$statusId}"),
]);
$post->user()->associate(Auth::user());
$post->replies()->save($post);

new:

$reply = Post::create([
   'body' => $request->input("reply-{$statusId}"),
]);
$post->user()->associate(Auth::user());
$post->replies()->save($reply);
Friday, August 20, 2021
 
Mark Ransom
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :