M
M
Maxim Spiridonov2015-03-12 16:31:21
Laravel
Maxim Spiridonov, 2015-03-12 16:31:21

How to correctly specify the namespace of the model so that there are no errors in laravel 5?

Hello! There was a problem in using the 5th lara, namely, I can not turn to the model.
I have a NewsController with the following content

<?php namespace App\Http\Controllers;

use Input;
use Validator;
use App\News as News;

class NewsController extends Controller {

  public function __construct()
  {
    $this->middleware('auth');
  }

  public function index()
  {
    return view('news');
  }

  public function addnews()
  {
    $data = input::all();

    $validator = Validator::make($data, array('title' => 'required|min:3', 'body' => 'required|min:3'));

    if($validator->fails())
    {
      return redirect()->back()->withErrors($validator->messages(), 'addnews');

    }
    else
    {
      $addnews = \News::add();
      return 'Новость успешно добавлена!';
    }	
  }

}

I'm trying to access the News
$addnews = \News::add();
model. The model itself is:
<?php namespace App;

class News extends Model {

  public static function add($data)
  {
    $news = new News();
    $news->title = $data['title'];
    $news->body = $data['body'];
    $news->save();
  }
  
}

An error occurs here:
FatalErrorException in NewsController.php line 32:
Class 'News' not found

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2015-03-12
@JhaoDa

You have a model in the App space, and you are trying to access it from the global namespace.
Either refer
or add at the beginning of the file
and refer to model
A, writing
this is redundant.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question