Support the ongoing development of Laravel.io →
Database IOC Laravel.io

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!

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

davidxd33 davidxd33 Joined 29 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.