我想在foreach循环laravel 5.6中使用if语句

时间:2018-06-18 08:43:57

标签: laravel-5.6

因为我是Laravel的新手所以面对这个问题并以其他方式尝试但是它不起作用。 这是我的刀片文件

products.blade.php

@foreach($products as $product)                                     

    <div class="women">
        <h6><a href="route{{'product.single}}">{{$product->title}}</a></h6>

        <span class="size">XL / XXL / S </span>

        <p ><em class="item_price">Rs.{{$product->price}}</em></p>


    </div>


    @if(count($product) ==  3)                                              
        <div class="clearfix"></div>
    @endif

@endforeach

为什么这不起作用

@if(count($product) ==  3)                                              
    <div class="clearfix"></div>
@endif

或者我如何在迭代中计算产品并比较if语句中的计数?

4 个答案:

答案 0 :(得分:0)

如果您有多于或少于3个产品,则您的if语句永远不会显示。您正在寻找的是products数组中的第三项,您可以这样做:

<?php

namespace App\Http\Controllers\API\Auth;

use App\Http\Controllers\API\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Support\Facades\Password;
use App\Model\Customer;

use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use GuzzleHttp\Client;
use DB;
class ResetPasswordController extends Controller
{
    use ResetsPasswords;
    protected $confirmEmail = false;
    public function __construct()
    {

    }

    public function resetPasswordCustomer(Request $request)
    {
        //Custome password reset mail has done in customer model
        $v = validator($request->only('token', 'password','password_confirmation'), [
            'token' => 'required|string|max:255',
            'password' => 'required|string',
            'password_confirmation' => 'required|string|min:6',
            ]);
        if ($v->fails()) {
            return response()->json(["error"=>$v->errors()->all()], 400);
        }
        else
        {
            $request->merge(['email'=> $this->checkTokenAgainstHashedValue($request->token)]);//Add new property to request.. 
            $response = $this->broker()->reset(
                $this->credentials($request), function ($user, $password) {
                    $this->resetPassword($user, $password);
                });
        }
        return $response == Password::PASSWORD_RESET
        ? $this->authToken($request)
        : $this->customResetFailedResponse($request, $response);

        return $response ;
    }
    protected function checkTokenAgainstHashedValue($token)
    {
        $resetTable = DB::table('password_reset_customers')->select('token','email')->get();
        foreach ($resetTable as $value) {
            if (Hash::check($token, $value->token)) {
                return $value->email;
            }
        }
        return null;
    }
    protected function authToken(Request $request)
    {
        $http = new Client();

        $response = $http->post('http://abcd.com/oauth/token', [
            'form_params' => [
            'grant_type' => 'password',
            'client_id' => 2,
            'client_secret' =>'qazrxGbDwrwbYXwbEbbkUFNO1zGB3eFYQN3AbG3m',
            'username' => Auth::user()->email,
            'password' =>  $request->password,
            'scope' => '',
            ],
            ]);

        return json_decode((string) $response->getBody(), true);

    }
    public function broker()
    {
        return Password::broker('customers');
    }

}

答案 1 :(得分:0)

要获取循环的索引,请使用

@foreach ($athletes as $key=>$athlete)

// Some html goes here

{ { ++$key } }

@endforeach

if条件添加到key

让我知道它是怎么回事

答案 2 :(得分:0)

数组计数在循环内部或外部始终相同。

如果你想根据当前的迭代做出决定,例如。 &#34;在每个第三个产品上放置一个clearfix div&#34;,如果密钥是数字,则将条件应用于密钥。

Blade提供loop变量及iteration属性(从1开始)以帮助解决此问题,请参阅此处https://laravel.com/docs/5.6/blade#the-loop-variable

每三个产品的示例:

@foreach($products as $product)
...

  @if ($loop->$loop->iteration%3 == 0)
        <div class="clearfix"></div>
  @endif

...
@endforeach

仅限第三种产品的示例:

  @if ($loop->$loop->iteration == 3)
        <div class="clearfix"></div>
  @endif

答案 3 :(得分:0)

您可以像这样使用loop variable

而不是:

@if($loop->iteration ==  3)                                              
    <div class="clearfix"></div>
@endif

你应该使用:

@if($loop->iteration % 3 == 0)                                              
    <div class="clearfix"></div>
@endif

但很可能在每3个元素(3,6,9等)之后你可能需要它,所以也许更好的解决方案是:

count($product)

你的例子没有用,因为$ product只是一个对象,所以count($products)没有预期的价值。此外,如果您使用if let locationJSON = response.result.value { let locationObject: Dictionary = locationJSON as! Dictionary<String, Any> self.dataArray = locationObject["data"]as! NSArray } (通知尾随s),它将无法工作,因为每次循环迭代中产品的数量都是相同的。