0

I've found many question realated to my problem but couldn't found an answer yet. It's about my foreach loop in my blade.

I want to print all product-names in my blade but I couln't figure out how to do that.

thats how I'm getting the products:

--- current code:

// controller 

    $id_array = Input::get('id');
    $products= Products::whereIn('id', $id_array)->get();
    $product_name = [];
    foreach($products as $arr)
    {
        $product_name=  $arr->lists('name');
    }

returning $product_name gives me this as a output:

["football","cola","idontknow","freshunicorn","dummy-data"]

In my blade is just a simple:

@foreach($products as $product)
    {{ $product}}
@endforeach

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

Thanks for your help and time.

3 Answers 3

1

It seems you are getting an object in an array in an array.

Like this:

array(
   array(
    object
  )
)

It happens because you use the get() function to retrieve you model. The get() function always "wants" to retrieve multiple models. Instead you will have to use the first() function.

Like this:

foreach($id_array as $arr)
{
    $want2editarray[] = Product::where('id', $arr)->first();
}

Hope it helps :)

Edit after @Wellno comment

That's probably because Product::where('id', $arr)->first(); returns null because it did not find anything.

I forgot to add a check after the retrieving of the product.

This can be done like this:

foreach($id_array as $arr)
{
    // First try to get model from database
    $product = Product::where('id', $arr)->first();

    // If $product insert into array
    if ($product) $want2editarray[] = $product;
}
Sign up to request clarification or add additional context in comments.

6 Comments

the controller output is the same, thats good. If I want to print $product->name in my blade, I'm getting an trying to get property of non-object
Edited my answer to fix your problem @WellNo
I allready did that :) but this haven't solved my problem.. my blade still gives me a : trying to get property of non-object -- I will update my question with my current code :)
There is a typo in your code :). You are using $products in your if statement and filling your array with $products. Use $product without and 's'.
oh, sorry, that was a mistake by me.. in my controller it's just product, I don't know how the 's' got in there :D
|
1

Why do you use loop with IDs? You can find all products by IDs:

$products = Product::whereIn('id', $id_array)->get();

And then use $products in the blade template

@foreach($products as $product)
    {{ $product->name }}
@endforeach

6 Comments

I tried it exactly like you told me but this just gives me an error too : htmlentities() expects parameter 1 to be string, array given
I've updated my question with current, much shorter code -- but still the same error - htmlentities() expects parameter 1 to be string, array given
Please, show $product variable. Before {{ $product->name }} write <?php dd($product); ?>
now I've updated it, I think there is everything we need :)
I allready tried that but this just gives me : implode(): Invalid arguments passed -- and why $product->name? I allready have all the names I want from the lists function. | implode(',', $product_name); is what I did
|
0

try to use Model/Eloquent to fetch data. View should only display the data and not fetching directly from DB or do heavy calculations.

1 Comment

my view doesn't fetch any data directly from my database. I'm getting what I want in my controller and sending all the data to my view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.