简体   繁体   中英

Laravel 5.2 model static functions use

I probably do not understand how these static functions work.

In my company's project they just create a public static function in any model and then they are using it this way modelname::functionName() . I think their models and controllers look similar to mine.

Now, I am training programming at home, I have created a Character model in App directory. Then I tried to use it the same way in CharacterController but it says:

FatalErrorException in CharacterController.php line 18: Class 'App\\Http\\Controllers\\Character' not found .

For me it looks like Laravel was searching for the static function in controller instead of my model.

My model:

<?php

namespace App;

use app;
use Illuminate\Database\Eloquent\Model;

class Character extends Model
{
    protected $table = 'character';

    public static function insertCharacterRace($race){
        DB::insert('INSERT INTO `character` VALUES `race` = ?', array($race));
    }

}

My controller:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use App;

class CharacterController extends Controller
{


    public function raceSelected($race){
        Character::insertCharacterRace($race);
    }

    }

In your controller change this:

use App;

To this:

use App\Character;

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