divHelper11 divHelper11 - 3 months ago 13
PHP Question

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);
}

}

Answer

In your controller change this:

use App;

To this:

use App\Character;