Reputation: 609
hi m trying to show data from db
to index page
but it says: Call to undefined function App\Http\Controllers\Post()
controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class IndexController extends Controller
{
public function index()
{
$data = Post();
return view('index', compact('data'));
//return view('index');
}
how can i solve it
Upvotes: 1
Views: 9374
Reputation: 521
$data = Post();
add all() method to this line
$data = Post::all();
Upvotes: 0
Reputation: 686
You are trying to call Post()
as a method/function, not the Model Post, and since Post() doesn't exist, you get this error.
In order to return all posts, using your Post Model, you should write it like this:
$data = Post::all(); // To return all posts
Or if you want to filter it based on some field on the database
$data = Post::where('active', 1)->get(); // Get all active posts (you need to adjust to any field you have on your database)
Your code could be something like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class IndexController extends Controller
{
public function index()
{
$data = Post::all();
return view('index', compact('data'));
}
Please, read more about eloquent here: https://laravel.com/docs/5.8/eloquent
Upvotes: 2
Reputation: 1446
The right syntax should be:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class IndexController extends Controller
{
public function index(Request $request)
{
$data = $request->all(); //get all input
//$data = $request->input('testing'); //$_POST['testing']
return view('index', ['data' => '$data']);
//return view('index', compact('data'));
//return view('index');
}
docs: https://laravel.com/docs/5.8/requests#accessing-the-request
Upvotes: 0