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.
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();
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.