Request for How to Handling if Command in Relationship Handling

Request for How to Handling if Command in Relationship Handling

Hello, good day Artisan,
Please, I need help with an if command not working for a relationship. I want to get the correct relationship for hashCard, but only the default relationship works. The if statement is not checking the status field of the UserHashCard model, which uses an enum ('available', 'initiated', 'joined', 'completed').

UserHashCard Model Code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class UserHashCard extends Model {
protected $guarded = [];
public function hashCard() {
return $this->belongsTo(HashCard::class);
}
// Uncomment if needed
// public function exchanges()
// {
// return $this->hasMany(HashCardExchange::class, 'hash_card_id');
// }
public function initiatedExchanges()
{
return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'hash_card_id');
}
public function joinedExchanges()
{
return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'join_card_id');
}
public function exchanges()
{
if ($this->status == 'initiated') {
return $this->initiatedExchanges();
} elseif ($this->status == 'joined') {
return $this->joinedExchanges();
}
// Default relationship (e.g., for 'available' or other statuses)
return $this->initiatedExchanges();
}
}
Controller Code
php
Copy code
public function getHashCards() {
$user = User::find(Auth::user()->id);
$hashCards = $this->allCards($user->id);
return response()->json($hashCards);
}
private function allCards($userid)
{
$user = User::find($userid);
// Fetch UserHashCards with their exchanges and associated usernames
$hashCards = UserHashCard::query()
->where('user_id', $user->id)
->with([
'hashCard', // Existing relationship
'exchanges' => function ($query) {
$query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
}
])
->orderByRaw("
CASE
WHEN status = 'initiated' THEN 1
WHEN status = 'joined' THEN 2
ELSE 3
END
")
->get();
return $hashCards;
}
</code>
<code><?php namespace AppModels; use IlluminateDatabaseEloquentModel; class UserHashCard extends Model { protected $guarded = []; public function hashCard() { return $this->belongsTo(HashCard::class); } // Uncomment if needed // public function exchanges() // { // return $this->hasMany(HashCardExchange::class, 'hash_card_id'); // } public function initiatedExchanges() { return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'hash_card_id'); } public function joinedExchanges() { return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'join_card_id'); } public function exchanges() { if ($this->status == 'initiated') { return $this->initiatedExchanges(); } elseif ($this->status == 'joined') { return $this->joinedExchanges(); } // Default relationship (e.g., for 'available' or other statuses) return $this->initiatedExchanges(); } } Controller Code php Copy code public function getHashCards() { $user = User::find(Auth::user()->id); $hashCards = $this->allCards($user->id); return response()->json($hashCards); } private function allCards($userid) { $user = User::find($userid); // Fetch UserHashCards with their exchanges and associated usernames $hashCards = UserHashCard::query() ->where('user_id', $user->id) ->with([ 'hashCard', // Existing relationship 'exchanges' => function ($query) { $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users` } ]) ->orderByRaw(" CASE WHEN status = 'initiated' THEN 1 WHEN status = 'joined' THEN 2 ELSE 3 END ") ->get(); return $hashCards; } </code>
<?php
namespace AppModels;

use IlluminateDatabaseEloquentModel;

class UserHashCard extends Model {
    protected $guarded = [];

    public function hashCard() {
        return $this->belongsTo(HashCard::class);
    }

    // Uncomment if needed
    // public function exchanges()
    // {
    //     return $this->hasMany(HashCardExchange::class, 'hash_card_id');
    // }

    public function initiatedExchanges()
    {
        return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'hash_card_id');
    }

    public function joinedExchanges()
    {
        return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'join_card_id');
    }

    public function exchanges()
    {
        if ($this->status == 'initiated') {
            return $this->initiatedExchanges();
        } elseif ($this->status == 'joined') {
            return $this->joinedExchanges();
        }

        // Default relationship (e.g., for 'available' or other statuses)
        return $this->initiatedExchanges();
    }
}

Controller Code
php
Copy code
public function getHashCards() {
    $user = User::find(Auth::user()->id);

    $hashCards = $this->allCards($user->id);

    return response()->json($hashCards);
}

private function allCards($userid)
{
    $user = User::find($userid);

    // Fetch UserHashCards with their exchanges and associated usernames
    $hashCards = UserHashCard::query()
        ->where('user_id', $user->id)
        ->with([
            'hashCard', // Existing relationship
            'exchanges' => function ($query) {
                $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
            }
        ])
        ->orderByRaw("
            CASE 
                WHEN status = 'initiated' THEN 1
                WHEN status = 'joined' THEN 2
                ELSE 3
            END
        ")
        ->get();

    return $hashCards;
}

1

I was facing almost the same problem some time ago. I wanted to fetch users with active orders, and this is how it worked:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function getUsersWithActiveOrders() {
$users = User::with('orders')->get();
$users->transform(function ($user) {
$user->active_orders = $user->orders->filter(function ($order) {
return in_array($order->status, ['pending', 'shipped']);
});
return $user;
});
return response()->json($users);
}
</code>
<code>public function getUsersWithActiveOrders() { $users = User::with('orders')->get(); $users->transform(function ($user) { $user->active_orders = $user->orders->filter(function ($order) { return in_array($order->status, ['pending', 'shipped']); }); return $user; }); return response()->json($users); } </code>
public function getUsersWithActiveOrders() {
$users = User::with('orders')->get();
$users->transform(function ($user) {
    $user->active_orders = $user->orders->filter(function ($order) {
        return in_array($order->status, ['pending', 'shipped']);
    });
    return $user;
});
return response()->json($users);
}

In your case, I think you are not using the relationship you defined. Here is how it should be:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function conditionalExchanges() {
if ($this->status == 'initiated') {
return $this->initiatedExchanges;
} elseif ($this->status == 'joined') {
return $this->joinedExchanges;
}
return collect();
}
</code>
<code>public function conditionalExchanges() { if ($this->status == 'initiated') { return $this->initiatedExchanges; } elseif ($this->status == 'joined') { return $this->joinedExchanges; } return collect(); } </code>
public function conditionalExchanges() {
    if ($this->status == 'initiated') {
        return $this->initiatedExchanges;
    } elseif ($this->status == 'joined') {
        return $this->joinedExchanges;
    }
    return collect();
}

Now in your controller, call the realtionship:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function getHashCards() {
$user = User::find(Auth::user()->id);
$hashCards = $this->allCards($user->id);
$hashCards->each(function ($hashCard) {
$hashCard->conditional_exchanges = $hashCard->conditionalExchanges();
});
return response()->json($hashCards);
}
</code>
<code>public function getHashCards() { $user = User::find(Auth::user()->id); $hashCards = $this->allCards($user->id); $hashCards->each(function ($hashCard) { $hashCard->conditional_exchanges = $hashCard->conditionalExchanges(); }); return response()->json($hashCards); } </code>
public function getHashCards() {
$user = User::find(Auth::user()->id);
$hashCards = $this->allCards($user->id);

$hashCards->each(function ($hashCard) {
    $hashCard->conditional_exchanges = $hashCard->conditionalExchanges();
});

return response()->json($hashCards);
}

and for the allCards function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$hashCards = UserHashCard::query()
->where('user_id', $user->id)
->with('hashCard')
->orderByRaw("
CASE
WHEN status = 'initiated' THEN 1
WHEN status = 'joined' THEN 2
ELSE 3
END
")
->get();
</code>
<code>$hashCards = UserHashCard::query() ->where('user_id', $user->id) ->with('hashCard') ->orderByRaw(" CASE WHEN status = 'initiated' THEN 1 WHEN status = 'joined' THEN 2 ELSE 3 END ") ->get(); </code>
$hashCards = UserHashCard::query()
    ->where('user_id', $user->id)
    ->with('hashCard')
    ->orderByRaw("
        CASE 
            WHEN status = 'initiated' THEN 1
            WHEN status = 'joined' THEN 2
            ELSE 3
        END
    ")
    ->get();

Let me know, if there is a problem after this.

@Hefer thank alot, i was able to make it work by modifying the allCards method in controller like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> private function allCards($userid)
{
$user = User::find($userid);
// Fetch UserHashCards with their exchanges and associated usernames
$userHashCards = UserHashCard::query()
->where('user_id', $user->id)
->with([
'hashCard', // Existing relationship
'initiatedExchanges' => function ($query) {
$query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
},
'joinedExchanges' => function ($query) {
$query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
}
])
->orderByRaw("
CASE
WHEN status = 'initiated' THEN 1
WHEN status = 'joined' THEN 2
ELSE 3
END
")
->get();
// Dynamically map exchanges based on card status and remove extra fields
$userHashCards->transform(function ($card) {
$card->exchanges = match ($card->status) {
'initiated' => $card->initiatedExchanges,
'joined' => $card->joinedExchanges,
default => collect(),
};
// Remove initiated_exchanges and joined_exchanges fields
unset($card->initiatedExchanges, $card->joinedExchanges);
return $card;
});
return $userHashCards;
}
</code>
<code> private function allCards($userid) { $user = User::find($userid); // Fetch UserHashCards with their exchanges and associated usernames $userHashCards = UserHashCard::query() ->where('user_id', $user->id) ->with([ 'hashCard', // Existing relationship 'initiatedExchanges' => function ($query) { $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users` }, 'joinedExchanges' => function ($query) { $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users` } ]) ->orderByRaw(" CASE WHEN status = 'initiated' THEN 1 WHEN status = 'joined' THEN 2 ELSE 3 END ") ->get(); // Dynamically map exchanges based on card status and remove extra fields $userHashCards->transform(function ($card) { $card->exchanges = match ($card->status) { 'initiated' => $card->initiatedExchanges, 'joined' => $card->joinedExchanges, default => collect(), }; // Remove initiated_exchanges and joined_exchanges fields unset($card->initiatedExchanges, $card->joinedExchanges); return $card; }); return $userHashCards; } </code>
   private function allCards($userid)
   {
       $user = User::find($userid);
   
       // Fetch UserHashCards with their exchanges and associated usernames
       $userHashCards = UserHashCard::query()
           ->where('user_id', $user->id)
           ->with([
               'hashCard', // Existing relationship
               'initiatedExchanges' => function ($query) {
                   $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
               },
               'joinedExchanges'  => function ($query) {
                   $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
               }
           ])
           ->orderByRaw("
               CASE 
                   WHEN status = 'initiated' THEN 1
                   WHEN status = 'joined' THEN 2
                   ELSE 3
               END
           ")
           ->get();
   
           // Dynamically map exchanges based on card status and remove extra fields
           $userHashCards->transform(function ($card) {
               $card->exchanges = match ($card->status) {
                   'initiated' => $card->initiatedExchanges,
                   'joined' => $card->joinedExchanges,
                   default => collect(),
               };
       
               // Remove initiated_exchanges and joined_exchanges fields
               unset($card->initiatedExchanges, $card->joinedExchanges);
       
               return $card;
           });
       
     return $userHashCards;
   }

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Request for How to Handling if Command in Relationship Handling

Request for How to Handling if Command in Relationship Handling

Hello, good day Artisan,
Please, I need help with an if command not working for a relationship. I want to get the correct relationship for hashCard, but only the default relationship works. The if statement is not checking the status field of the UserHashCard model, which uses an enum ('available', 'initiated', 'joined', 'completed').

UserHashCard Model Code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class UserHashCard extends Model {
protected $guarded = [];
public function hashCard() {
return $this->belongsTo(HashCard::class);
}
// Uncomment if needed
// public function exchanges()
// {
// return $this->hasMany(HashCardExchange::class, 'hash_card_id');
// }
public function initiatedExchanges()
{
return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'hash_card_id');
}
public function joinedExchanges()
{
return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'join_card_id');
}
public function exchanges()
{
if ($this->status == 'initiated') {
return $this->initiatedExchanges();
} elseif ($this->status == 'joined') {
return $this->joinedExchanges();
}
// Default relationship (e.g., for 'available' or other statuses)
return $this->initiatedExchanges();
}
}
Controller Code
php
Copy code
public function getHashCards() {
$user = User::find(Auth::user()->id);
$hashCards = $this->allCards($user->id);
return response()->json($hashCards);
}
private function allCards($userid)
{
$user = User::find($userid);
// Fetch UserHashCards with their exchanges and associated usernames
$hashCards = UserHashCard::query()
->where('user_id', $user->id)
->with([
'hashCard', // Existing relationship
'exchanges' => function ($query) {
$query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
}
])
->orderByRaw("
CASE
WHEN status = 'initiated' THEN 1
WHEN status = 'joined' THEN 2
ELSE 3
END
")
->get();
return $hashCards;
}
</code>
<code><?php namespace AppModels; use IlluminateDatabaseEloquentModel; class UserHashCard extends Model { protected $guarded = []; public function hashCard() { return $this->belongsTo(HashCard::class); } // Uncomment if needed // public function exchanges() // { // return $this->hasMany(HashCardExchange::class, 'hash_card_id'); // } public function initiatedExchanges() { return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'hash_card_id'); } public function joinedExchanges() { return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'join_card_id'); } public function exchanges() { if ($this->status == 'initiated') { return $this->initiatedExchanges(); } elseif ($this->status == 'joined') { return $this->joinedExchanges(); } // Default relationship (e.g., for 'available' or other statuses) return $this->initiatedExchanges(); } } Controller Code php Copy code public function getHashCards() { $user = User::find(Auth::user()->id); $hashCards = $this->allCards($user->id); return response()->json($hashCards); } private function allCards($userid) { $user = User::find($userid); // Fetch UserHashCards with their exchanges and associated usernames $hashCards = UserHashCard::query() ->where('user_id', $user->id) ->with([ 'hashCard', // Existing relationship 'exchanges' => function ($query) { $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users` } ]) ->orderByRaw(" CASE WHEN status = 'initiated' THEN 1 WHEN status = 'joined' THEN 2 ELSE 3 END ") ->get(); return $hashCards; } </code>
<?php
namespace AppModels;

use IlluminateDatabaseEloquentModel;

class UserHashCard extends Model {
    protected $guarded = [];

    public function hashCard() {
        return $this->belongsTo(HashCard::class);
    }

    // Uncomment if needed
    // public function exchanges()
    // {
    //     return $this->hasMany(HashCardExchange::class, 'hash_card_id');
    // }

    public function initiatedExchanges()
    {
        return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'hash_card_id');
    }

    public function joinedExchanges()
    {
        return $this->hasMany(HashCardExchange::class, 'hash_card_id', 'join_card_id');
    }

    public function exchanges()
    {
        if ($this->status == 'initiated') {
            return $this->initiatedExchanges();
        } elseif ($this->status == 'joined') {
            return $this->joinedExchanges();
        }

        // Default relationship (e.g., for 'available' or other statuses)
        return $this->initiatedExchanges();
    }
}

Controller Code
php
Copy code
public function getHashCards() {
    $user = User::find(Auth::user()->id);

    $hashCards = $this->allCards($user->id);

    return response()->json($hashCards);
}

private function allCards($userid)
{
    $user = User::find($userid);

    // Fetch UserHashCards with their exchanges and associated usernames
    $hashCards = UserHashCard::query()
        ->where('user_id', $user->id)
        ->with([
            'hashCard', // Existing relationship
            'exchanges' => function ($query) {
                $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
            }
        ])
        ->orderByRaw("
            CASE 
                WHEN status = 'initiated' THEN 1
                WHEN status = 'joined' THEN 2
                ELSE 3
            END
        ")
        ->get();

    return $hashCards;
}

1

I was facing almost the same problem some time ago. I wanted to fetch users with active orders, and this is how it worked:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function getUsersWithActiveOrders() {
$users = User::with('orders')->get();
$users->transform(function ($user) {
$user->active_orders = $user->orders->filter(function ($order) {
return in_array($order->status, ['pending', 'shipped']);
});
return $user;
});
return response()->json($users);
}
</code>
<code>public function getUsersWithActiveOrders() { $users = User::with('orders')->get(); $users->transform(function ($user) { $user->active_orders = $user->orders->filter(function ($order) { return in_array($order->status, ['pending', 'shipped']); }); return $user; }); return response()->json($users); } </code>
public function getUsersWithActiveOrders() {
$users = User::with('orders')->get();
$users->transform(function ($user) {
    $user->active_orders = $user->orders->filter(function ($order) {
        return in_array($order->status, ['pending', 'shipped']);
    });
    return $user;
});
return response()->json($users);
}

In your case, I think you are not using the relationship you defined. Here is how it should be:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function conditionalExchanges() {
if ($this->status == 'initiated') {
return $this->initiatedExchanges;
} elseif ($this->status == 'joined') {
return $this->joinedExchanges;
}
return collect();
}
</code>
<code>public function conditionalExchanges() { if ($this->status == 'initiated') { return $this->initiatedExchanges; } elseif ($this->status == 'joined') { return $this->joinedExchanges; } return collect(); } </code>
public function conditionalExchanges() {
    if ($this->status == 'initiated') {
        return $this->initiatedExchanges;
    } elseif ($this->status == 'joined') {
        return $this->joinedExchanges;
    }
    return collect();
}

Now in your controller, call the realtionship:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function getHashCards() {
$user = User::find(Auth::user()->id);
$hashCards = $this->allCards($user->id);
$hashCards->each(function ($hashCard) {
$hashCard->conditional_exchanges = $hashCard->conditionalExchanges();
});
return response()->json($hashCards);
}
</code>
<code>public function getHashCards() { $user = User::find(Auth::user()->id); $hashCards = $this->allCards($user->id); $hashCards->each(function ($hashCard) { $hashCard->conditional_exchanges = $hashCard->conditionalExchanges(); }); return response()->json($hashCards); } </code>
public function getHashCards() {
$user = User::find(Auth::user()->id);
$hashCards = $this->allCards($user->id);

$hashCards->each(function ($hashCard) {
    $hashCard->conditional_exchanges = $hashCard->conditionalExchanges();
});

return response()->json($hashCards);
}

and for the allCards function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$hashCards = UserHashCard::query()
->where('user_id', $user->id)
->with('hashCard')
->orderByRaw("
CASE
WHEN status = 'initiated' THEN 1
WHEN status = 'joined' THEN 2
ELSE 3
END
")
->get();
</code>
<code>$hashCards = UserHashCard::query() ->where('user_id', $user->id) ->with('hashCard') ->orderByRaw(" CASE WHEN status = 'initiated' THEN 1 WHEN status = 'joined' THEN 2 ELSE 3 END ") ->get(); </code>
$hashCards = UserHashCard::query()
    ->where('user_id', $user->id)
    ->with('hashCard')
    ->orderByRaw("
        CASE 
            WHEN status = 'initiated' THEN 1
            WHEN status = 'joined' THEN 2
            ELSE 3
        END
    ")
    ->get();

Let me know, if there is a problem after this.

@Hefer thank alot, i was able to make it work by modifying the allCards method in controller like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> private function allCards($userid)
{
$user = User::find($userid);
// Fetch UserHashCards with their exchanges and associated usernames
$userHashCards = UserHashCard::query()
->where('user_id', $user->id)
->with([
'hashCard', // Existing relationship
'initiatedExchanges' => function ($query) {
$query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
},
'joinedExchanges' => function ($query) {
$query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
}
])
->orderByRaw("
CASE
WHEN status = 'initiated' THEN 1
WHEN status = 'joined' THEN 2
ELSE 3
END
")
->get();
// Dynamically map exchanges based on card status and remove extra fields
$userHashCards->transform(function ($card) {
$card->exchanges = match ($card->status) {
'initiated' => $card->initiatedExchanges,
'joined' => $card->joinedExchanges,
default => collect(),
};
// Remove initiated_exchanges and joined_exchanges fields
unset($card->initiatedExchanges, $card->joinedExchanges);
return $card;
});
return $userHashCards;
}
</code>
<code> private function allCards($userid) { $user = User::find($userid); // Fetch UserHashCards with their exchanges and associated usernames $userHashCards = UserHashCard::query() ->where('user_id', $user->id) ->with([ 'hashCard', // Existing relationship 'initiatedExchanges' => function ($query) { $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users` }, 'joinedExchanges' => function ($query) { $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users` } ]) ->orderByRaw(" CASE WHEN status = 'initiated' THEN 1 WHEN status = 'joined' THEN 2 ELSE 3 END ") ->get(); // Dynamically map exchanges based on card status and remove extra fields $userHashCards->transform(function ($card) { $card->exchanges = match ($card->status) { 'initiated' => $card->initiatedExchanges, 'joined' => $card->joinedExchanges, default => collect(), }; // Remove initiated_exchanges and joined_exchanges fields unset($card->initiatedExchanges, $card->joinedExchanges); return $card; }); return $userHashCards; } </code>
   private function allCards($userid)
   {
       $user = User::find($userid);
   
       // Fetch UserHashCards with their exchanges and associated usernames
       $userHashCards = UserHashCard::query()
           ->where('user_id', $user->id)
           ->with([
               'hashCard', // Existing relationship
               'initiatedExchanges' => function ($query) {
                   $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
               },
               'joinedExchanges'  => function ($query) {
                   $query->with(['user:id,username']); // Eager load only the `id` and `username` fields from `users`
               }
           ])
           ->orderByRaw("
               CASE 
                   WHEN status = 'initiated' THEN 1
                   WHEN status = 'joined' THEN 2
                   ELSE 3
               END
           ")
           ->get();
   
           // Dynamically map exchanges based on card status and remove extra fields
           $userHashCards->transform(function ($card) {
               $card->exchanges = match ($card->status) {
                   'initiated' => $card->initiatedExchanges,
                   'joined' => $card->joinedExchanges,
                   default => collect(),
               };
       
               // Remove initiated_exchanges and joined_exchanges fields
               unset($card->initiatedExchanges, $card->joinedExchanges);
       
               return $card;
           });
       
     return $userHashCards;
   }

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật