简体   繁体   English

使用:: find但带有自定义where子句

[英]Using ::find but with a custom where clause

I use ::find to find the business but business_position I need to use WHERE business_id = and WHERE id to find the correct position, and example is avalible below... 我使用::find查找业务,但是business_position我需要使用WHERE business_id =和WHERE id来找到正确的位置,下面提供了示例...

<?php
    $s = Stats::find(Auth::user()->id);
    $myJob = \App\Businesses::find($s->business_id);

    $matchedValues = ['business_id' => $s->business_id, 'id' => $s->business_position];
    $myPosition = \App\BusinessPositions::where($matchedValues)->get();
    ?>

And using $myPosition I use 然后使用$myPosition

{{ $myPosition->pay_amount }}

And on that line it throws and error... 在那条线上它会引发错误...

Undefined property: Illuminate\\Database\\Eloquent\\Collection::$pay_amount (View: C:\\Users\\Darren\\MyLavProject\\resources\\views\\pages\\business\\business.blade.php) 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ pay_amount(视图:C:\\ Users \\ Darren \\ MyLavProject \\ resources \\ views \\ pages \\ business \\ business.blade.php)

Laravel's get() returns a collection of results that match your query (even if there are none or just one, it'll be an array of Laravel model instances). Laravel的get()返回与查询匹配的结果集合 (即使没有或只有一个,它将是Laravel模型实例的数组)。

You can use first() instead of get() to grab the first result, or you can foreach through each of the results get() provides. 您可以使用first()而不是get()来获取第一个结果,或者可以foreach get()提供的每个结果。 It depends if your application permits more than one permission per business_id / id combination. 这取决于您的应用程序是否允许每个business_id / id组合使用多个权限。

$myPosition This is an array of objects. $myPosition这是一个对象数组。

So if you need the result: 因此,如果您需要结果:

{{ $myPosition[0]->pay_amount }}

Or 要么

@foreach($myPosition as $position)
  {{ $position->pay_amout }}
@endforeach

Of if you don't want to use any of these: 如果您不想使用以下任何一种:

Change your query to : 将查询更改为:

$myPosition = \App\BusinessPositions::where($matchedValues)->first();

Now you can use: {{ $myPosition->pay_amount }} 现在您可以使用: {{ $myPosition->pay_amount }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM