samedi 1 mai 2021

How to appending a column db result with relation and some logic laravel

I have a problem here, I want to add one column from the result of the relation, namely the percentage column, this column is the result of pagebook / pageread * 100 this is my result db what I want

"id": 2,
    "id_book": 2,
    "id_user": 2,
    "pageread": 120,
    "books": {
                "id": 2,
                "pagebook":125
             }
    "percentage":96

this is my code in controller

$book= Read::with('books.authors')->where('id_user',$user->id)->get();

and this my code in model

public function books()
    {
        return $this->belongsTo(Book::class,'id_book','id');
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire