Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

smeunus's avatar
Level 12

Selecting two values from the same table but under different conditions?

// I want to get two record. One record has to be promoted and the other is not.

$students = Student::where('promoted', 1)
            ->where('promoted', 0) // what should I do here?
            ->take(2)
            ->get();

0 likes
8 replies
Tray2's avatar

If you want both promoted 0 or 1 you can use ->orWhere('promoted', 0)

$students = Student::where('promoted', 1)
            ->orWhere('promoted', 0) 
            ->get();

or you can use whereIn

$students = Student::whereIn('promoted',[0, 1])
            ->take(2)
            ->get();
1 like
smeunus's avatar
Level 12

@tray2 thanks for your reply. In my case I want two record, where one has to be promoted and other is not.

where and orWhere checks either promoted or not promoted. So it doesn't ensure me that it will return one with promoted result and other with non promoted.

Sinnbeck's avatar

You would have to write some sort of alternative sql manually to do this.

Here is an example https://stackoverflow.com/questions/8986942/alternating-rows-based-on-a-value-of-a-column-in-mysql

Another solution would be to simply make 2 queries using first()

$student_promoted = Student::where('promoted', 1)->first();
$student_not_promoted = Student::where('promoted', 0)->first();
$students = collect([$student_promoted, $student_not_promoted]);
1 like
sujancse's avatar

Why not filter after fetching the data like

$students = Student::get();
$promoted = $student->where('promoted', 1)->first();
$notPromoted = $student->where('promoted', 0)->first();

this will save you a query

Or groupBy promoted then grab the first data of each group

1 like
AbdulBazith's avatar

@smeunus

for what reason you are trying to do like that?

or else just fetch $students = Student::get();

and in you view file use a if condition based on your requirement

like

@foreach($students as $student)

@if($student->promoted ==1)
//your code
@else
//your code
@endif
@endforeach

But what you expecting either only two records from table, or all records from table.

1 like
smeunus's avatar
Level 12

@abdulbazith

Imagine

  • you buy a product from my store.
  • I will give you two gift (recharge, and tshirt).
  • Those gifts are stored in a gifts table where type - rechargeand tshirt
  • I have so many gift types ['recharge', 'tshirt', 'stickers', 'food']
  • I want to ensure that if you buy a Shirt from my store I will give you two gift. mobile recharge and a T-shirt.
nionta's avatar

I think in this case you could use have some changes in your database structure and I think it would be very easy to execute what you were saying in your last comment; you could have a table name gift_names and another table name product_names and a mid table combine those things like product_gifts which has two column e.g. product_id,gift_id; then when you select the product, you could only show those gifts which were selected for that specific product like:

            public function get_gifts_according_to_product(Request $request)
                {$gifts = DB::table('product_gifts')
                ->join('product_names','product_gifts.product_id','=','product_names.id')
                ->join('gift_names','product_gifts.gift_id','=','gift_names.id')
                ->where('product_names.id',$request('product_id'))
                ->get();}

I am just suggesting you.

1 like
smeunus's avatar
Level 12

Thank you for your suggestion. I'll give it a try.

Please or to participate in this conversation.