How could I query (find) by the "name" attribute in Laravel/PHP?
one text
I'm trying to follow this: Find or Create with Eloquent
and the findOrCreate public function would work perfectly, but even tho I have "getRouteKeyName()" right above the findOrCreate, using Tag::find() will look for the given data as an ID, not as the name.
Should I make a completely new and custom PHP file with a function called findOrCreate, or there's a way I can tell this function to look-up by name and not by id?
public function getRouteKeyName()
{
return 'name';
}
public static function findOrCreate(array $tags){ <---- contains strings "default", "example", "other"
return Tag::find($tags[0]); <---- this only accepts IDs, but $tags[0] points to "default", not an ID. Obviously, it should look up the database via the name.
}
Am I missing something?
Source