Как я могу получить значения из двух Laravel таблиц, используя l oop на мой взгляд - PullRequest
0 голосов
/ 09 января 2020

Следующая строка возвращает все записи из двух таблиц, потому что я добавил отношение для этих двух таблиц.

$allmarketers =  ProductStock::whereRaw('MONTH(created_at) = ?',[$currentMonth])->get();

Теперь я хочу провести l oop через записи в представлении и отобразить записи из двух таблиц для каждого пользователя. Я забыл, как этого добиться.

Я пытался

@foreach($allmarketers as $item)

Но как мне получить значения обеих таблиц внутри этого l oop?

Var_dump

n)#601 (1) { ["items":protected]=> array(3) { [0]=> object(App\ProductStock)#602 (26) { ["table":protected]=> string(14) "product_stocks" ["fillable":protected]=> array(5) { [0]=> string(8) "marketer" [1]=> string(6) "target" [2]=> string(6) "_token" [3]=> string(10) "updated_at" [4]=> string(10) "created_at" } ["connection":protected]=> string(5) "mysql" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(9) { ["id"]=> int(97) ["user_id"]=> int(70) ["marketer"]=> string(18) "Samson Samson | 70" ["target"]=> string(3) "100" ["_token"]=> NULL ["status"]=> int(1) ["vehicle"]=> string(10) "Unassigned" ["created_at"]=> string(19) "2020-01-07 13:11:47" ["updated_at"]=> string(19) "2020-01-07 13:11:47" } ["original":protected]=> array(9) { ["id"]=> int(97) ["user_id"]=> int(70) ["marketer"]=> string(18) "Samson Samson | 70" ["target"]=> string(3) "100" ["_token"]=> NULL ["status"]=> int(1) ["vehicle"]=> string(10) "Unassigned" ["created_at"]=> string(19) "2020-01-07 13:11:47" ["updated_at"]=> string(19) "2020-01-07 13:11:47" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } [1]=> object(App\ProductStock)#603 (26) { ["table":protected]=> string(14) "product_stocks" ["fillable":protected]=> array(5) { [0]=> string(8) "marketer" [1]=> string(6) "target" [2]=> string(6) "_token" [3]=> string(10) "updated_at" [4]=> string(10) "created_at" } ["connection":protected]=> string(5) "mysql" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(9) { ["id"]=> int(98) ["user_id"]=> int(72) ["marketer"]=> string(22) "Emmanuel Increase | 72" ["target"]=> string(3) "150" ["_token"]=> NULL ["status"]=> int(1) ["vehicle"]=> string(10) "Unassigned" ["created_at"]=> string(19) "2020-01-07 13:11:47" ["updated_at"]=> string(19) "2020-01-07 13:11:47" } ["original":protected]=> array(9) { ["id"]=> int(98) ["user_id"]=> int(72) ["marketer"]=> string(22) "Emmanuel Increase | 72" ["target"]=> string(3) "150" ["_token"]=> NULL ["status"]=> int(1) ["vehicle"]=> string(10) "Unassigned" ["created_at"]=> string(19) "2020-01-07 13:11:47" ["updated_at"]=> string(19) "2020-01-07 13:11:47" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } [2]=> object(App\ProductStock)#604 (26) { ["table":protected]=> string(14) "product_stocks" ["fillable":protected]=> array(5) { [0]=> string(8) "marketer" [1]=> string(6) "target" [2]=> string(6) "_token" [3]=> string(10) "updated_at" [4]=> string(10) "created_at" } ["connection":protected]=> string(5) "mysql" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(9) { ["id"]=> int(99) ["user_id"]=> int(73) ["marketer"]=> string(18) "Timothy Kukah | 73" ["target"]=> string(3) "200" ["_token"]=> NULL ["status"]=> int(1) ["vehicle"]=> string(10) "Unassigned" ["created_at"]=> string(19) "2020-01-07 13:11:47" ["updated_at"]=> string(19) "2020-01-07 13:11:47" } ["original":protected]=> array(9) { ["id"]=> int(99) ["user_id"]=> int(73) ["marketer"]=> string(18) "Timothy Kukah | 73" ["target"]=> string(3) "200" ["_token"]=> NULL ["status"]=> int(1) ["vehicle"]=> string(10) "Unassigned" ["created_at"]=> string(19) "2020-01-07 13:11:47" ["updated_at"]=> string(19) "2020-01-07 13:11:47" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } } }

1 Ответ

0 голосов
/ 09 января 2020

вы можете использовать это ниже. надеюсь, что это может помочь вам. это простой код. Я полагаю, у вас есть идентификатор столбца в вашей БД.

<table class="table is-striped">
        @if(count($allmarketers) > 0)
        <thead>
          <tr>
            <th>id</th>
            <th>date</th>
          </tr>
        </thead>
          @endif
 <tbody>
 @foreach ($allmarketers as $item)
          <tr>
            <td>{{$item->id}}</td>
            <td>{{$item->created_at}}</td>
          </tr>
 @endforeach
 </tbody>
</table>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...