如何在Laravel中使用foreach循环打印键值对[已关闭]

elcex8rz  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(116)
    • 已关闭**。此问题需要超过focused。当前不接受答案。
    • 想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

25天前关闭。
Improve this question
我正在尝试从控制器发送一个数组(包含数据,如名字,姓氏,url等)到blade。现在我需要在blade文件中使用foreach打印它。

    • 控制器代码**
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
    • 我的问题是如何打印单个值的?**
ctehm74n

ctehm74n1#

您的代码是一个烂摊子。这里是一个更干净的版本,没有使用模型(因为您没有说,如果他们到位)

    • 控制器代码**
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);
}

相关问题