简体   繁体   中英

Why my laravel5.2 will showing undefine methods in DB class

I create a class and extends it form model. now i need to call mysql stored procedure which i already made in mysql database but from this model class when i use DB::select it shows undefine method. after using use Illuminate\\Support\\Facades\\DB; the problem is still there.

my code is:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Domain extends Model
{
    public $table = 'english_news';

    public static function getEngNews()
    {
        return DB::select("call getUserNews");
    }
}

From the documents this is the proper way you would use Laravel query builder to do a select

$users = DB::table('users')->select('name', 'email as user_email')->get(); as another example $query = DB::table('users')->select('name');

Your code seems to be trying to call another function in your query which won't work in Laravel that I'm aware of. Query builder is meant to give you some flexibility in building a query in the Eloquent structure.

Have you tried using DB::statement('call getUserNews'); ? This is the recommended way to write hardcoded sql statements using the Laravel's DB facade. Take a look at the statement section in https://laravel.com/docs/5.2/database#running-queries .

Open command line for creating one model:

php artisan make:model ModelName

Now one file will create in App folder. Open that file,

namespace App;
use Illuminate\Database\Eloquent\Model;
class ModalName extends Model
{
 protected $table = 'english_news'; //Fill this portion.
}

In Controller:

public static function getEngNews()
{
    return ModalName::select("getUserNews");
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM