V
V
vvmgev2015-08-10 18:17:23
Laravel
vvmgev, 2015-08-10 18:17:23

How to connect model to controller in laravel5?

class Index extends Controller
{
  /**
   * Display a listing of the resource.
   *
   * @return Response
   */
  public function index()
  {

    return View('index');
    
  }

but how to connect the model???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-08-10
@alexey-m-ukolov

class Index extends Controller
{
  /**
   * Display a listing of the resource.
   *
   * @return Response
   */
  public function index()
  {
    $models = MyModel::all();
    return View('index', compact('models'));
  }
}

V
Vyacheslav Plisko, 2015-08-11
@AmdY

Read documentation laravel.com/docs/5.1/controllers#dependency-inject...

// инъекция в конструктор 
    public function __construct(User $users) {
        $this->users = $users;
    }
    public function index() {
        return $this->user->all();
    }
// инъекция прямо в метод
   public function index(User $users) {
        return $users->all();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question