1

How can I access a model from a controller using laravel4?

So far I have my controller:

<?php
class GatewayController extends BaseController {

public function getContentAll()
{
    $content = Content::getContentAll();
    return $content;

}

And my model:

<?php

class Content extends Eloquent {

    protected $table = '_content';

    public function getContentAll(){

        return 'test';
    }

But I get:

Whoops, looks like something went wrong.
2
  • What error did you got exactly ? Commented Aug 29, 2013 at 11:04
  • First thing - you are doing a static call to the model's function whereas it is not declared as static. Commented Aug 29, 2013 at 11:33

2 Answers 2

1

Firstly, Eloquent handles the returning of a model collection. You do not need to handle this yourself. So your model should simply look like this:

class Content extends Eloquent {

    protected $table = '_content';

}

You can then simply get all your content using this:

$content = Content::all();

EDIT:

If you want to do stuff with the data in your model, try this:

class Content extends Eloquent {

    protected $table = '_content';

    public function modifiedCollection()
    {
        $allContent = self::all();
        $modifiedContent = array();

        foreach ($allContent as $content) {
            // do something to $content                  

            $modifiedContent[] = $content;
        }

        return $modifiedContent;
    }  
}

This should then work:

$content = Content::modifiedCollection();
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, I know, but I want to do something to the data before returning it to the controller. Which should be done in a model.
Your edited solution does not work either, it's basically the same code I have in my original question.
Well, perhaps you should set debug to true and tell us what the error message is. This should work unless something else is amiss.
@user1013512 mate take a look at the Eloquent Accessors/Mutators if you want to process data between being fetched from the DB and being transfered to the Controller (or whatever else). But one remark, this concernes columns/fileds not collections.
@Gadoma these won't process what I want to be processed
|
0

Instead of this:

$content = Content::getContentAll();

Try this:

$content = Content->getContentAll();
                  ^^

Or declare your function as static, like so:

public static function getContentAll(){

    return 'test';
}

UPDATE: if you don't want to have a static function, you should instantiate your class in order to call non-static functions:

$c = new Content();
$content = $c->getContentAll();

5 Comments

-> this does not work and I do not want to declare it as static
Thanks, just out of interest, which method would be better? Static or non-static? Why use one over the other?
Well I think @cuewizchris's answer is the way to go. You shouldn't put business logic in your model.
@simone and why is that ? It's not a missuse to follow the "slim-controller" philosophy, in which most of the Biz Logic is placed in the models.
I can't really say if the slim controller (or fat model) philosophy is good in Laravel; honestly I don't know much about it. However I think we should just try to follow Laravel's own philosophy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.