【问题标题】:How to print key value pair in Laravel using foreach loop [closed]如何使用 foreach 循环在 Laravel 中打印键值对 [关闭]
【发布时间】:2023-02-08 03:21:41
【问题描述】:

我正在尝试将数据数组(包含数据,即名字、姓氏、url 等)从控制器发送到刀片服务器。现在我需要使用每个刀片打印它。以下是控制器和刀片代码:

控制器代码

   public function show_friend_request()
    {

        $user_id = auth()->user()->id;


        $get_friend_requests = DB::table('friendships')->where('recipient_id', $user_id)->where('status', 'pending')->get(['sender_id']);

        
        $array = json_decode($get_friend_requests, true);

        $count_friend_Request =  count($array);
      
         for($i=0; $i<$count_friend_Request; $i++)
         {
            $show_friend_request_Data[] = DB::table('users')
            ->leftJoin('friendships', 'friendships.sender_id', '=', 'users.id')
            ->where('users.id', '=', $array[$i])
            ->get(['first_name','last_name']);
         }
          
          return view('pages.friend_request')->with('Active_Request',$show_friend_request_Data);

    }

刀片代码

  @foreach($Active_Request as $friend => $value)
          <li>
            <div class="rounded badge-unread d-sm-flex border-0 mb-1 p-3 position-relative">
              <!-- Avatar -->
              <div class="avatar text-center">
                <img class="avatar-img rounded-circle" src="assets/images/avatar/01.jpg" alt="">
              </div>
              <!-- Info -->
              <div class="mx-sm-3 my-2 my-sm-0">
                <p class="small mb-2"><b>{{$friend}} : {{$value}}}</b> sent you a friend request.</p>
              <!-- Button -->
              <div class="d-flex">
                <button class="btn btn-sm py-1 btn-primary me-2">Accept </button>
                <button class="btn btn-sm py-1 btn-danger-soft">Delete </button>
              </div>
            </div>
            </div>
          </li>
          @endforeach

问题是当我尝试像“$friend->first_name”一样打印时它会抛出以下错误

Property [first_name] does not exist on this collection instance.

但是,如果我打印“{{$friend}}”,它会按如下方式打印整个数组,

我的问题是如何打印个人价值?

【问题讨论】:

  • users 表有以下列 id email dob password first_name last_name username gender phone country Friendships 表有以下列 id sender_id recipent_id status created_at updated_at

标签: laravel eloquent foreach laravel-8 laravel-blade


【解决方案1】:

你的代码一团糟。这是一个没有使用模型的更清晰的版本(因为你没有说它们是否到位)

控制器代码

public function show_friend_request()
{
    $user_id = auth()->user()->id;

    $senderIds = DB::table('friendships')->where('recipient_id', $user_id)->where('status', 'pending')->pluck('sender_id')->toArray();
    
    $activeRequests = DB::table('users')
        ->whereIn('id', $senderIds)
        ->get(['first_name','last_name']);

    return view('pages.friend_request')->with('activeRequest', $activeRequests);
}

刀片代码

@foreach($activeRequest as $key => $friend)
    <li>
        <div class="rounded badge-unread d-sm-flex border-0 mb-1 p-3 position-relative">
            <!-- Avatar -->
            <div class="avatar text-center">
                <img class="avatar-img rounded-circle" src="assets/images/avatar/01.jpg" alt="">
            </div>
            <!-- Info -->
            <div class="mx-sm-3 my-2 my-sm-0">
                <p class="small mb-2"><b>{{$key}} : {{$friend->first_name.' '.$friend->last_name}}</b> sent you a friend request.</p>
                <!-- Button -->
                <div class="d-flex">
                    <button class="btn btn-sm py-1 btn-primary me-2">Accept </button>
                    <button class="btn btn-sm py-1 btn-danger-soft">Delete </button>
                </div>
            </div>
        </div>
    </li>
@endforeach

控制器中的请求可以融合为一个

public function show_friend_request()
{
    $user_id = auth()->user()->id;
    
    $activeRequests = DB::table('users')
        ->leftJoin('friendships', 'friendships.sender_id', '=', 'users.id')
        ->where('friendships.recipient_id', $user_id)
        ->where('friendships.status', 'pending')
        ->get(['first_name','last_name']);

    return view('pages.friend_request')->with('activeRequest', $activeRequests);
}

如果你有适当的模型和关系,你可以做

public function show_friend_request()
{
    $activeRequests = User::whereHas('friendships', function ($friendship) {
        $friendship->where('recipient_id', auth()->id())
            ->where('status', 'pending');
    })->get(['first_name','last_name']);

    return view('pages.friend_request')->with('activeRequest', $activeRequests);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多