Skip to content
This repository has been archived by the owner on Mar 29, 2020. It is now read-only.

[How to] Use a different custom Review model for each reviewable #16

Closed
clarkewing opened this issue Mar 24, 2018 · 3 comments
Closed

Comments

@clarkewing
Copy link

While playing around with the package, I found myself where I needed to use reviews for two different models. However, I also wanted to use a custom Review model for each of these reviewables.

The current implementation, through the config file only allows me to override the Review model globally. I figured out a way to do this individually for each reviewable and thought I'd share how I did it so others might be able to do the same.

On the reviewable model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Naoray\LaravelReviewable\Traits\HasReviews;

class TravelTip extends Model
{
    use HasReviews;

    /**
     * Override HasReviews trait method to use appropriate model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function reviews()
    {
        return $this->morphMany(
            'App\TravelTipReview', // Replace this with your custom Review model
            'reviewable'
        );
    }

On your custom Review model:

<?php

namespace App;

use Naoray\LaravelReviewable\Models\Review;

class TravelTipReview extends Review
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'reviews'; // Required for Laravel to avoid trying to resolve the table name from the model name

    // Add your custom properties and methods here

All that's left to do is call your custom Review model is controllers if needed!

@Naoray
Copy link
Owner

Naoray commented Mar 24, 2018

Hey @clarkewing,
Thanks for sharing your configuration! I should ve included a “how to use your own review model“ in the readme file - will add this soon.

I d suggest you to instead of overwriting the reviews function of the hasReviews trait, just replace the rewieable.models.review config with a reference to your own model and you are good to go.

@clarkewing
Copy link
Author

Hey @Naoray,

Thanks! The reason I chose to go about it this way is because replacing the rewieable.models.review config applies the custom model globally, and I need to use different custom models for different reviewables.

For example, in my case I need to implement HashSlugs for both TravelTipReviews and HotelReviews but I also need to implement Spatie's Media Library package only for TravelTipReviews, but not for HotelReviews which don't use images.

That's why I needed separate custom Review models.

Again, thanks for sharing this package, it's great!

@Naoray
Copy link
Owner

Naoray commented Mar 24, 2018

Ok, I see. I'll link this issue in the readme.

:) Thanks!

@Naoray Naoray closed this as completed Mar 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants