For months, I've been placing all my logic in my controllers. Today, I would like to steer clear of this route and creating models for my logic and simply call them in my controllers. Let's think of it this way, I have a control panel to edit game records in a database. In my controllers, I just continuously build up queries and add, remove, update, and delete records that way.
What I would like to do is something like this:
Game::id('game-name')->update('description', 'new description');
Game::id('game-name')->remove('description');
Game::id('game-name')->description // returns description
I find this far more efficient.
This is models/Game.php
<?php
class Game{
protected $properties = [];
protected $statistics = [];
private $game;
public function __construct() {
$query = DB::connection('website')->table('gametypes')
->where('name', $this->game)
->get();
if($query) {
// Let's assign some properties to this game..
$this->properties['name'] = $query->name;
}
}
public function id($game) {
$this->game = $game;
return $this;
}
public function name() {
$this->properties['name'];
return $this;
}
}
What I am trying to do here is:
Game::id('game-name')->name() // return name
If it's far more complicated than this, please don't simply reply with links. I do need an explanation. Thanks!
Is there a particular reason that you aren't using Eloquent? This seems like a good use case.
Create a table named "games" and make the schema how you wish. You can then create an Eloquent model like so:
class Game extends Eloquent {
//by default, you don't even need anything else here
}
You are then able to do all sorts of things like finding a game by ID:
Game::find(1)->name; //returns the name of game 1
You can find more information about Eloquent here: http://laravel.com/docs/eloquent
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community