M
M
Makfroy2017-03-07 12:02:00
Laravel
Makfroy, 2017-03-07 12:02:00

Is it possible to work with two models in one controller?

I'm trying to display the number of records in two different Laravel 5.2 databases, one is displayed, and the other is not. Where could you go wrong? In routes?

use App\User;
use App\Post;
use Session;

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $users = User::paginate(5);
        $posts = Post::paginate(5);
        return view('test', compact('users','posts'));
    }

But when I insert into the view: It
{{ count($posts) }}
gives an error:
Undefined variable: posts

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2017-03-07
@4Geka

This is exactly the error:
I would do like this:

return view('test', ['users' => $users,'posts' => $posts]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question