0

I have made a custom class, which is a Facade. Here is the class:

<?php namespace MT\Library;

class Posting
{
    function Draft($inputs)
    {
        return ($this->create($inputs, 0)) ? true : false;
    }

    private function create ($inputs, $type)
    {
        $post = new Illuminate\Database\Post();
        $post->title = $inputs["title"];

        return ($post->save()) ? true : false;
    }
}

Posting() is the name of my custom class.

Post()

is the name of my Laravel Eloquent Model. When I use the class Postin::Draft() it throws an error that cannot find the model new Illuminate\Database\Post();

How should I use my model in my custom library? Since my library uses a namespace I cannot simply do Post() for model, as it throws an error telling cannot find Post()

1
  • Could you try using $post = new \Illuminate\Database\Post(); and check as to what the result is? Commented Apr 15, 2014 at 12:20

1 Answer 1

2

If it is in the models folder then use:

$post = new \Post();

Also you may add use Post at the top of your class.

Whenever using a class from another namespace use \ before the class name (including namespace if exists) you are referring; to indicate that, it's in the global namespace, otherwise PHP will look under the current namespace.

Sign up to request clarification or add additional context in comments.

Comments

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.