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

Categories

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

php - Get data from array depend on database integer

I have a model of Posts with status column. I wonder, what would be be practice in Laravel to retrieve from something like 0 or 1(stored integers in DB) and instead of that show "displayed" or "hidden"? Not inside of the blade temples but when doing something like:

        return response()->json(['posts' => $posts])

"status" of $posts would be not "0" but "displayed"?

question from:https://stackoverflow.com/questions/65901744/get-data-from-array-depend-on-database-integer

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

1 Reply

0 votes
by (71.8m points)

You can make it in Posts Model:

const displayed  = 1;
const hidden     = 0;
public static function status()
    {
        return [
            self::displayed  => 'displayed',
            self::hidden     => 'hidden',
        ];
    }

And retrieve it.

$post = [
"status" => Posts::status()[$request->status]
];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...