1

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

    }

1 Answer 1

3

In your controller change this:

use App;

To this:

use App\Character;
Sign up to request clarification or add additional context in comments.

4 Comments

Damn, next error haha: Class 'App\DB' not found. Thank you for your answer :)
It doesnt help. Is it possible that I dont have this DB file in my project or something? It is new installed laravel
You added use DB; in model and you still get same error? Alternatively, you can change DB:: to \DB:: - in this case you will not need to add use DB; clause. BTW, you don't need to use static, just use public function insertCharacterRace($race).
Damn I added it in controller instead of class... Thank you a lot!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.