You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Mar 29, 2020. It is now read-only.
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!
The text was updated successfully, but these errors were encountered:
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.
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!
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:
On your custom Review model:
All that's left to do is call your custom Review model is controllers if needed!
The text was updated successfully, but these errors were encountered: