0

I'm trying to display names from the database using Ajax and I use foreach loop in the controller but it only returns one name instead of two(it doesn't loop correctly). I have seen some answers, peoples suggested to use foreach in the view but for my case I use Ajax in the view how can I use the foreach in Ajax or is there any way it can display all names?

I have tried using these, but it was returning one name instead of two.

$data = [];
$data[] = $review->user->name; 

Controller

  $products = Product::where('id','=',$id)->with('reviews.user')->get();
        foreach ($products as $product)
        {
            foreach ($product->reviews as $review){
                $data =  $review->user->name;
                dd($data); //This returns one name
            }
        }

Ajax

 <script >
 function userRatingname() {
    $.ajax({
        type: "GET",
        url: '{{route('userRating.name ', $id)}}',
        success: function(data) {
            $('#userRatingname').html('<div>' + data + '</div>');

        }
    });

}
userRatingname(); 
</script>
0

3 Answers 3

2

You are overwriting the value of $data again and again, so it will away return the last user name.

You need to put the $data = []; out of loop. and use $data[] = $review->user->name; inside the loop:

$products = Product::where('id','=',$id)->with('reviews.user')->get();
$data = array(); // defined the $data here out of loop
foreach ($products as $product)
{
   foreach ($product->reviews as $review){
       $data []= $review->user->name; // push the username to $data   
   }
}
// Or you can use whereIn for User:
$data = User::whereIn('id', ProductReview::where('product_id',$id)->pluck('user_id'))->pluck('name')->toArray();

return response()->json(['code' => 200, 'data' => $data]);

Change your ajax code:

function userRatingname() {
    $.ajax({
        type: "GET",
        url: '{{route('userRating.name ', $id)}}',
        success: function(data) {
            var html = '';
            data['data'].forEach(function(name) {
                html += '<div>' + name + '</div>'
            });
            $('#userRatingname').html(html);
        }
    });
Sign up to request clarification or add additional context in comments.

14 Comments

I tried that before and now this is the output Array:1 [▼ 0 => "Ethan" ] only one name @TsaiKoga
@user11710915 are us sure about that product's reviews has more than one user?
Yeah am sure because the product has two reviews with different users @TsaiKoga
@user11710915 what about Review::where('product_id', $id)->pluck('user_id'), did it return two user_ids
yeah #Items: Array:2 [▼ 0 => 1 1 => 4 ] @TsaiKoga
|
0

try this one if its right tell me.

     $data = [];//define empty array.


    $products = Product::where('id','=',$id)->with('reviews.user')->first();


        foreach ($product->reviews as $review){
            $data[]= [$review->user->name];

        }

i understand your issue you are using variable not array so in loop u use array and use[your storing value]

4 Comments

The output is Array:1 [▼ 0 => Array:1 [▼ 0 => "Ethan" ] ] @M.Idrish
[email protected] send me your code i will solve your problem
Can you just solve it here so that it will be helpful for others too. @ M.Idrish
ok i want to see dd($products = Product::where('id','=',$id)->with('reviews.user')->first()); comment me full output of this dd
0

You have forgot to add the "csrf" tag in your ajax call

Comments

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.