Yea that is good idea. Create PetController with those methods and define protected $model; which you will define in other controller for example in DogController.
BTW: Are you from Slovakia?
Thanks nipid for your answer. What do you mean by defining proteced $model? Why?
Yes I am from Slovakia :)
I imagine your going to get a lot of overlap between your different animal models. Why not create one animal table and have 3 different models extending 1 abstract animal model
<?php
// Animal.php
abstract class Animal extends \Illuminate\Database\Eloqent\Model
{
protected $table = 'animals'
const DOG_ID = 1;
const CAT_ID = 2;
const BIRD_ID = 3;
public function newQuery($excludeDeleted = true)
{
return parent::newQuery()->species($this->getSpeciesId());
}
public function scopeSpecies($query, $species)
{
return $query->where('species', '=', $species);
}
protected abstract function getSpeciesId();
}
// Dog.php
class Dog extends Animal
{
public function getSpeciesId()
{
return self::DOG_ID;
}
}
// Cat.php
class Cat extends Animal
{
public function getSpeciesId()
{
return self::CAT_ID;
}
}
// Bird.php
class Bird extends Animal
{
public function getSpeciesId()
{
return self::BIRD_ID;
}
}
That might need some refining to make it work. I just quickly wrote that now. I think it will work.
Thanks damienadermann,
that seems like a very nice idea, I just dont know If I want to change the database in this stage. I know I did not give it a proper though at the beginning about the tables structure of pets.
Well, decisions decisions :/
TorchSK said:
Thanks damienadermann,
that seems like a very nice idea, I just dont know If I want to change the database in this stage. I know I did not give it a proper though at the beginning about the tables structure of pets.
Well, decisions decisions :/
Still have solution for you here :)
interface Animal {
/**
* The animal specy
*
* @return string
*/
public function specy();
/**
* The animal sound
*
* @return string
*/
public function sound();
}
class Dog extends Eloquent implements Animal {
public function specy()
{
return 'Dog';
}
public function sound()
{
return 'Whoof!';
}
}
// So on for cat, monkey ;)
Then you can have your AnimalController dealing with AnimalInterface, instead of specific model type. Like:
class PetController extends Controller
{
public function listenToThe($specy)
{
$pet = App::make($specy);
return "Oh! The { $pet->specy() } sound: { $pet->sound() }";
}
}
And @damienadermann:
It's not scalable to the application if you define your Animal like that. When you need add more animal type to the database, then you have to define a new constant in the Animal class.
For further approach: with my implementation, you'll face the problem - how to create an instance of the AnimalInterface in the controller. Using App::make() as I do above is a quick and simple way, but it will lead the application to an error if the $specy parameter is not proper, so you can write your own AnimalFactory or AnimalResolver as well.
@bomberman1990, yesss, that is exactly what I am trying to implement now. I am not very familiar with how IoC works so, I'll need a little bit of studying :)
Thanks a lot.
I just "hate" (in fact I love it) how Laravel makes me to write my code properly!!! :P
Because I'm from Slovakia also if you need some help follow me at twitter @nipidqo and pm me :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community