Home > Mobile >  Can't get result from where condition with variables in laravel
Can't get result from where condition with variables in laravel

Time:02-28

this is how i'm trying to get the type of certificate with where condition , but still recieve nothing from this query:

$res= student::find($student, ['typecertificate']); 
$k = certificate::select('id-cer')->where('name-cer','=',$res)->get(); 
return $k;

CodePudding user response:

Based on your comments, I'm assuming you want to retrieve the field certificateType from the latest record that was inserted in the students table.

You can achieve that without a where clause, by directly using the Eloquent Builder to retrieve only that specific field like this:

Student::latest()->first('certificateType');

But this would give you an Eloquent Collection with one element. If you just want the value (not wrapped in a collection), you can simply retrieve the latest student and get the corresponding field directly:

$certificateType = Student::latest()->first()->certificateType;

I could explain more, but your question is vague and your database schema isn't clear either, so I'd need more information on that as well as what you intend to achieve.

In any case, Laravel's documentation is often a big help: https://laravel.com/docs/9.x/eloquent#retrieving-single-models

  • Related