调用未定义的方法错误

时间:2015-07-11 15:57:40

标签: laravel eloquent laravel-5

获取以下错误。不知道如何解决它。我可能有错误的地方,因为这是我的第一个laravel项目所以我不确定我是否正确地做了。

BadMethodCallException in Builder.php line 1994:
Call to undefined method Illuminate\Database\Query\Builder::getLewpByName()

EloquentLewpRepository的内容

<?php namespace App\Repositories\Frontend\Lewp;

use App\Lewp;   
use App\Exceptions\GeneralException;

/**
 * Class EloquentUserRepository
 * @package App\Repositories\Lewp
 */
class EloquentLewpRepository implements LewpContract {



    /**
     * @param $id
     * @return \Illuminate\Support\Collection|null|static
     * @throws GeneralException
     */
    public function findOrThrowException($id) {
        $lewp = Lewp::find($id);
        if (! is_null($lewp)) return $lewp;
        throw new GeneralException('That lewp does not exist.');
    }

    /**
     * @param $data
     * @param bool $provider
     * @return static
     */

    public function create($data) {
        $lewp = Lewp::create([
            'name' => $data['name'],
            'title' => $data['title'],
            'text' => $data['text'],
            'sidebar' => $data['sidebar'],
            'submission_text' => $data['submission_text'],
            'type' => $data['type'],
            'content_options' => $data['content_options'],
            'link_button_text' => $data['link_button_text'],
            'text_button_text' => $data['text_button_text'],
            'options' => $data['options'],
            'comment_sort_method' => $data['comment_sort_method'],
            'hide_comment_scores' => $data['hide_comment_scores'],
            'header_mouseover-text' => $data['header_mouseover-text']
        ]);

        return $lewp;
    }

    public function searchLewpsByName($term) {
        $lewp = Lewp::where('name', 'LIKE', $term)->get();

        return $lewp;
    }

    public function getLewpByName($lewpname) {
        $lewp = Lewp::where('name', '=', $lewpname)->first();

        return $lewp;
    }

    public function getLewpId($lewpname) {
        $lewp = Lewp::select(array('id')->where('name', '=', $lewpname)->first();

        return $lewp;
    }

}

App / Lewp的含义

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Lewp extends Model
{
    use SoftDeletes;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'lewps';

    /**
     * The attributes that are not mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * For soft deletes
     *
     * @var array
     */
    protected $dates = ['deleted_at'];   
}

控制器的内容

<?php namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Lewp;

/**
 * Class FrontendController
 * @package App\Http\Controllers
 */
class FrontendController extends Controller {

    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {
        return view('frontend.index');
    }

    /**
     * @return \Illuminate\View\View
     */
    public function macros()
    {
        return view('frontend.macros');
    }
    public function post()
    {
        return view('frontend.post');
    }
    public function exterior()
    {
        return view('frontend.exterior');
    }
    public function submit()
    {
        return view('frontend.submit');
    }
    public function self()
    {
        return view('frontend.self');
    }
    public function lewp($name)
    {
        if(strlen($name) == 0)
        {
            return view('frontend.index');
        }
        $lewp = Lewp::getLewpByName($name);
        return view::make('frontend.lewp', array('lewp' => $lewp));
    }
}

1 个答案:

答案 0 :(得分:0)

您在课程getLewpByName中创建方法EloquentLewpRepository,但在FrontendController中,您使用Lew调用Lewp::getLewpByName是模型。你明白问题吗?通过这种方法,您应该在documents上了解有关IOC的更多信息。

P / s:对不起我的英语。