mercredi 15 juin 2016

Get all fields from db model in Laravel

After creating model, when I try to get his attributes, i get only fields in database that are filled.

    ----------------------------------------------
DB: | id | shopID | name | bottleID | capacity |
    ----------------------------------------------
    | 1  | 8      | Cola |  3       |          |
    ----------------------------------------------

In this case I need capacity attribute too, as empty string

public function getDrinkData(Request $request)
{
    $drink = Drink::where('shopId', $request->session()->get('shopId'))->first();

    if($drink) {
        $drink = $drink->attributesToArray();
    }
    else {
        $drink = Drink::firstOrNew(['shopId' => $request->session()->get('shopId')]);
        $drink = $drink->attributesToArray(); // i want to get even empty fields
    }
    return view('shop.drink')->(['drink' => $drink])
}

But for later usage (in view) I need to have all attributes, including empty ones. I know that this code works as it should, but I don't know how to change it to detect all attributes.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire