Home > Software engineering >  data limiting global scope with laravel model
data limiting global scope with laravel model

Time:07-31

i have a model named "Page" and I want the first of my model's image arm data to be written, I wonder how can I do it?

public function index($lang)
    {
        $data = Page::where('language', $lang)->orderBy('order', 'ASC')->with('categori')->get();
        if ($data) {
            return response()->json($data);
        }
        return response()->json(['message' => 'Record not found.'], 404);
    }
{
    "id": 3,
    "title": "Conporta",
    "contents": "<p>Conporta</p>",
    "slug": "hakkimizda",
    "image": [
        "/storage/page/Conporta/40-banner-1659203850.jpg",
        "/storage/page/Conporta/40-banner-small-1659203850.jpg",
        "/storage/page/Conporta/40-banner-medium-1659203850.jpg"
    ],
    "language": "en",
    "is_home": "false",
    "status": "on",
    "seo": null,
    "order": "0",
    "category_id": 2,
    "created_at": "2022-07-30T17:57:30.000000Z",
    "updated_at": "2022-07-30T17:57:30.000000Z",
}

CodePudding user response:

You can hide the image attribute from model and create new attribute that should hold one image.

...
protected $hidden = ['image'];

protected $appends = ['img'];

...

public function getImgAttribute(){
    return $this->image[0];
}
  • Related