0

I am building a Laravel Application where people read book, favorite the book also.

Book Resource

public function toArray($request)
{
   // return parent::toArray($request);
   return [
        'id' => $this->id,
        'name' => $this->name,
        'about' => $this->about,
        'content' => $this->content,
        'image' => $this->image_url,
        'recommended' => $this->recommended,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
        'author' => $this->author,
        'no_of_pages' => $this->pages,
   //    'favorited' =>  I want o show true or false depending if user favorited 
   ];
}

I want to set include Favorited Book by the user in the user model, so it shows true if the user has favorited and false.

I was able to do it in a single book model, by doing this

public function showapi(Request $request, $book)
{

    //$book = Book::find($book);
    $book  = new BookResource(Book::find($book));
    $Fav = Favorite::where('book_id', $book->id)->where('user_id', $request->user_id)->exists();
    if($Fav) {
    return response()->json([
        'book' => $book, 
        'Favorited' => $Fav,
        ]
    );
  }
  else 
  return response()->json([
    'book' => $book, 
    'Favorited' => $Fav,
    ]
);

}

This set Favorited to True or False

But I want to show Favorited when I send the collection of Books

I have tried using Laravel API Resource Relationship by doing this

    'item' => FavoriteResource::collection($this->when(Favorite::where('book_id', $this->id)
->where('user_id', $request->user_id), 'item')),

I got this error while using postman to test

Call to a member function first() on string in file

This is my method

public function indexapi()
{
  return BookResource::collection(Book::with('author')->Paginate(16));
}

This is my relationship

User Model

public function favorites(){
    return $this->hasMany(Favorite::class);
}

public function books()
{
    return $this->belongsToMany(Book::class);
}

Book Model

public function favorites()
{
    return $this->hasMany(Favorite::class);
}

** Favorite Model **

public function book ()
{
    return $this->belongsTo(Book::class);
}

public function author()
{
    return $this->belongsTo(Author::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}
2
  • Whats the relationship between user and books? Is it many to many? Commented Apr 4, 2020 at 10:53
  • There is no relationship between user and books. I have updated my question Commented Apr 4, 2020 at 11:02

2 Answers 2

1

I do not know if this is solution to your problem, but just want to edit your model as you missed a part in the book model

User model

    public function favorites(){
    return $this->hasMany(Favorite::class);
}

public function books()
{
    return $this->belongsToMany(Book::class);
}

Book Model

public function favorites()
{
    return $this->hasMany(Favorite::class);
}
public function User()
{
    return $this->hasOne(User::class);
}

Your favourited class

 public function book ()
{
    return $this->belongsTo(Book::class);
}

public function author()
{
    return $this->belongsTo(Author::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}
Sign up to request clarification or add additional context in comments.

1 Comment

sorry for that little error on books model, have edited it
0

You have a many to many relationship between your books and your users. The pairs are stored in the intermediate table book_user. Just add a boolean column favorite to the intermediate table book_user.

You may access it like this:

$user = App\User::find(1);

foreach ($user->books as $book) {
    echo $book->pivot->favorite;
}

Read more about the intermediate table here.

You may also want to create a model representing the intermediate table (this would be your favorite model).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.