Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

php - Is this a good practice? – Eloquent: Getting a table record from a specific column

I want to get an instance of an Eloquent model record based on a specific column that it is not the pk.

So in the controller I'm doing:

$boat = Boat::getByBoatName($boatName);

And in the Boat model I have:

    public static function getByBoatName($boatName)
    {
        return self::where('boat_name', $boatName)->first();
    }

Is this a good practice using self::where and it is a good practice the whole approach in general?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would just do in the controller:


public function getBoatByName($boatName){
   $boat = Boat::where('name', $boatName)->first();

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...