【问题标题】:Laravel iterate complex objectLaravel 迭代复杂对象
【发布时间】:2018-08-19 03:30:12
【问题描述】:

我在迭代对象(如下)时遇到了一些麻烦,因为它具有主要的 User 数据,然后是 profilejobs照片 数组。

我收到了错误Invalid argument supplied for foreach()

在此先感谢您的帮助,因为我似乎陷入了心理循环,找不到我做错了什么。

在 Controller i 中调用 DB:

$team = \App\User::where('users.state', 1)->with('profile','jobs','photos')->get();
$team->jsonSerialize();

我在视图中为携带给我错误的对象的变量循环所做的工作

            <li>
                <a href="/{!! str_slug(trans('menus.lawyers'), '-') !!}/{!! str_slug(trans('global.words.partners')) !!}" title="{!! trans('global.words.partners') !!}">{!! trans('global.words.partners') !!}</a>
                <ul class="dropdown">
                    @foreach($team as $member)
                        @foreach($member->jobs as $jobs)
                            // just display if partner | socio
                            @if ($jobs->slug== 'socio' || $jobs->slug == 'partner')
                                @foreach($member->profile as $profile)
                                    <li>
                                        <a href="/{!! str_slug(trans('global.words.lawyer_male'), '-') !!}/{!! $member->profile->full_name_slug !!}" title="{!! $member->profile->full_name !!}"><img class="avatar" src="{{ asset('/images/staff/thumbs/'.$member->photos[3]->filename) }}" alt="{!! $member->profile->full_name !!}"> {!! $member->profile->full_name !!}
                                        </a>
                                    </li>
                                @endforeach
                            @endif
                        @endforeach
                    @endforeach
                </ul>
            </li>

对象(简化版):

0 => array:15 [▼ "id" => 2 "guid" => "0b6e0ad2-7daa-4c2b-907a-d095ab577851" "name" => "John Doe" "slug" => "john-doe" "email" => "mp@testingsite.com" "state" => 1 "ordering" => 0 "created_by" => 1 "updated_by" => null "deleted_by" => null "created_at" => "2018-02-12 19:59:47" "updated_at" => "2018-02-12 19:59:52" "profile" => array:23 [▼ "id" => 2 "user_id" => 2 "full_name" => "John Doe McNamara" "full_name_slug" => "john-doe-mcnamara" "short_name" => "John Doe" "short_name_slug" => "john-doe-mcnamara" "gender" => "male" "birth_date" => "1973-05-05" "work_start" => "2000-01-01" "work_end" => null "biography" => "John Doe started the firm in 1900." "experience" => "A couple of years" "education" => """ UCLA;\n Berkley """ "affiliations" => "" "notes" => "" "hits" => 3 "state" => 1 "ordering" => 0 "created_by" => 1 "updated_by" => null "deleted_by" => null "created_at" => "2018-02-12 20:07:00" "updated_at" => "2018-02-24 12:32:52" ] "jobs" => array:2 [▼ 0 => array:15 [▼ "id" => 1 "guid" => "45f91a68-d219-42b1-8980-8cb77d6688b4" "type" => "Lawyer" "slug" => "lawyer" "department" => "Law" "notes" => null "language" => "en" "state" => 1 "ordering" => 4 "created_by" => 1 "updated_by" => null "deleted_by" => null "created_at" => "2018-02-11 01:43:04" "updated_at" => null "pivot" => array:2 [▼ "user_id" => 2 "job_id" => 1 ] ] 1 => array:15 [▼ "id" => 11 "guid" => "fcf9f310-eeb1-4ff4-b133-6c80d6bf3b7f" "type" => "Partner" "slug" => "partner" "department" => "Law" "notes" => null "language" => "en" "state" => 1 "ordering" => 1 "created_by" => 1 "updated_by" => null "deleted_by" => null "created_at" => "2018-02-11 01:49:38" "updated_at" => null "pivot" => array:2 [▼ "user_id" => 2 "job_id" => 11 ] ] ] "photos" => array:4 [▼ 0 => array:14 [▼ "id" => 5 "guid" => null "user_id" => 2 "type" => "profile" "directory" => "staff" "subdirectory" => "profile" "filename" => "john-doe@1-100.jpg" "state" => 1 "ordering" => 0 "created_by" => 1 "updated_by" => null "deleted_by" => null "created_at" => "2018-02-12 20:03:31" "updated_at" => null ] 1 => array:14 [▼ "id" => 6 "guid" => null "user_id" => 2 "type" => "bgs" "directory" => "staff" "subdirectory" => "bgs" "filename" => "john-doe@1-100.jpg" "state" => 1 "ordering" => 0 "created_by" => 1 "updated_by" => null "deleted_by" => null "created_at" => "2018-02-12 20:03:49" "updated_at" => null ] 2 => array:14 [▶] 3 => array:14 [▶] ] ] 1 => array:15 [▶] 2 => array:15 [▶] 3 => array:15 [▶] 4 => array:15 [▶] 5 => array:15 [▶] 6 => array:15 [▶] 7 => array:15 [▶] 8 => array:15 [▶] 9 => array:15 [▶] 10 => array:15 [▶] 11 => array:15 [▶] 12 => array:15 [▶] 13 => array:15 [▶] ]

【问题讨论】:

  • 我不知道为什么会这样。尝试找出哪个foreach 特别会导致该错误。但是你打电话给$team-&gt;jsonSerialize();有什么特别的原因吗?我不认为它在做任何事情。
  • 我尝试不调用任何方法并获取对象,尝试了 -&gt;toArray() 和现在的 -&gt;jsonSerialize(); 来确定这是否可能是原因之一。
  • 不要调用这些。查询会返回 Laravelcollection,这在客观上基本优于数组。 JSON 没有意义,因为您将它与 PHP 一起使用。另外,我很确定它实际上没有做任何事情,因为您需要明确覆盖 $users 对象。无论哪种方式,请尝试找出导致错误的foreach。您可能在错误消息中有缓存视图。在大多数现代文本编辑器中,您可以使用 Ctrl + P 导航到它并输入前几个字符。
  • 看起来@foreach($member-&gt;profile as $profile)循环出现错误
  • 为了让它工作,我发现我不需要@foreach($member-&gt;profile as $profile) 循环。那么我必须调用的是,例如,{!! $member-&gt;profile['full_name'] !!},这样它就可以工作了。

标签: laravel object foreach view iterate


【解决方案1】:

你得到一个你使用JsonSerializeed的错误

这是你可以使用的代码

$team = \App\User::where('users.state', 1)->with('profile','jobs','photos')->get();

在你看来,像你已经在做的那样迭代它

 <li>
                <a href="/{!! str_slug(trans('menus.lawyers'), '-') !!}/{!! str_slug(trans('global.words.partners')) !!}" title="{!! trans('global.words.partners') !!}">{!! trans('global.words.partners') !!}</a>
                <ul class="dropdown">
                    @foreach($team as $member)
                        @foreach($member->jobs as $jobs)
                            // just display if partner | socio
                            @if ($jobs->slug== 'socio' || $jobs->slug == 'partner')
                                @foreach($member->profiles as $profile)
                                    <li>
                                        <a href="/{!! str_slug(trans('global.words.lawyer_male'), '-') !!}/{!! $member->profile->full_name_slug !!}" title="{!! $member->profile->full_name !!}"><img class="avatar" src="{{ asset('/images/staff/thumbs/'.$member->photos[3]->filename) }}" alt="{!! $member->profile->full_name !!}"> {!! $member->profile->full_name !!}
                                        </a>
                                    </li>
                                @endforeach
                            @endif
                        @endforeach
                    @endforeach
                </ul>
            </li>

希望对你有帮助

【讨论】:

  • 虽然我同意如果他在没有明确覆盖$users 变量的情况下使用它可能会导致问题,但它不会做任何事情。如果他这样做了,第一个 foreach 应该可以工作,$member-&gt;jobs 应该抛出一个不同的错误(在非对象上调用方法)。
  • Laravel levaral,使用了您建议的代码,但仍然出现错误。
  • 似乎我现在得到错误的地方是@foreach($member-&gt;profile as $profile)
  • @McRui 是的,你是对的 @foreach($member->profile as $profile) 正在生成错误
猜你喜欢
  • 2020-05-14
  • 1970-01-01
  • 2011-05-23
  • 2020-01-05
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多