Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 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 Answer

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]
];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...