Skip to content
Advertisement

Laravel, how to display Values in DB with just given IDs?

im trying to get 5 data from DB with their ID ?

HTML

<input type="number" name="cid1">
<input type="number" name="cid2">
<input type="number" name="cid3">
<input type="number" name="cid4">
<input type="number" name="cid5">

Route

Route::post('/selectCampaign','EmailController@selectCampaign')->name('selectCampaign');

Controller – im Trying to get Just 1 ID and its not Working

public function selectCampaign(Request $request) 
    {
        $id1 = request('cid1');
        
        $offers = Offer::where('id','=', $id1);
        return view('email_add', compact('offers'));
    }

I want to show 5 Offers from Offers Table, with given 5 ID in HTML Form ? How can i do this in Laravel ?

Advertisement

Answer

You never execute your query… where() is a Query Builder instance, until you call ->first() or ->get(). If you want 1 record, then use first(), if you want multiple, then use get().

Or, in your case, you can use find():

$offer = Offer::where('id', '=', $request->input('cid1'))->first();
// OR
$offer = Offer::find($request->input('cid1'));

If you want to get all 5, then you’d call:

$offers = Offer::whereIn('id', [
  $request->input('cid1'),
  $request->input('cid2'),
  $request->input('cid3'),
  $request->input('cid4'),
  $request->input('cid5')
])->get();

Or, if no other input is being passed from your form:

$offers = Offer::whereIn('id', $request->except('_token'))->get();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement