如何在Laravel中添加模型方法?

时间:2013-08-19 09:39:33

标签: php laravel laravel-4

我的模型看起来像这样:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class Post extends Eloquent {
}

然后我尝试添加这样的方法:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class Post extends Eloquent {
    public static function create($type, $postid, $url, $author, $content)
    {
        $post = new Post;
        $post->type = $type;
        $post->postid = $postid;
        $post->url = $url;
        $post->author = $author;
        $post->content = $content;
        try
        {
        $post->save();
        echo $post;
        }catch(Exception $e){
            throw new Exception( 'Already saved', 0, $e);
        } 
    }
}

在控制器中,我试图引用这样的方法:

public function savepostController($type, $postid, $url, $author, $content)
{
    Post::create($type, $postid, $url, $author, $content);
}

当我运行控制器时,我得到Controller method not found.

1 个答案:

答案 0 :(得分:0)

尝试使用,这是一个例子

class Exports extends Eloquent {

    public static function my_exports($user)
    {
         // DB call here
    }
}

然后在你的路线中你可以做

Route::post('export/(:number)', function($user)
{

    $user_exports = Export::my_exports($user);

    // Do stuff here
});