Get current id of item without loop. Laravel, php

  eloquent, laravel, orm, php, web

let’s go to my problem. Suppose I have a table that is named Hero and now I want to get HeroId for the hero name batman(unique) for normal I will use a loop to check in the list hero if the hero that has a name is Batman so I got it. But with this way I must loop for too many times, this problem reminds me of the Auth::user->id the function has already been built by Laravel this function could get the id of any user that is currently on stage without using any loop. so What must I do to build like that?
Thanks, everyone.

public function get_name_byid($name_hero)
{
    return Hero::where('name_hero', $name_hero)->value('id');
}
Auth::user()->id

Source: Laravel

Leave a Reply