I am trying too check if a specific array contains a specific value with the in_array method. But for some reason it seems like it's not working.
@if (in_array(Auth::id(), $likedUsers))
<p>yes</p>
@endif
<?php echo Auth::id() ?>
<?php dd($likedUsers) ?>
The results of my 2 dumps:
As you can see the array contains the value.
That is not how in_array()
works. Your array contains objects and the objects are the things that contain the user id. in_array()
will check the exact elements of the array it doesn't check the properties of the elements (the objects in this case).
It does something like this:
$isLikedUser = false;
foreach ($likedUsers as $likedUser) {
if ($likedUser == Auth::id()) {
$isLikedUser = true;
}
}
But you need this:
$isLikedUser = false;
foreach ($likedUsers as $likedUser) {
if ($likedUser->user_id == Auth::id()) {
$isLikedUser = true;
}
}
You should do this in your controller and then you can pass the result to your view like this:
$view = View::make('viewname')->with('isLikedUser', $isLikedUser);
Note that you don't have to give the same name to your view variable when you use the with()
function, like: ->with('variableNameInView', $usedVariableValue);
Then your view will have a variable with the name of the first parameter that you can check:
@if ($isLikedUser)
<p>yes</p>
@endif
<?php echo Auth::id() ?>
<?php dd($likedUsers) ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.