Reputation: 1175
I'm new to Laravel and the documentation's basic task list returns Views from the Route(web.php
) but I want to use a Controller to return an image file.
So I have for my route:
Route::get('/products', 'ProductController@index');
Then my ProductController action (please ignore comments as I'm using index
to simplify things):
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
@return \Illuminate\Http\Response
Fetch and return all product records.
*/
public function index()
{
//
//return response()->json(Product::all(), 200);
return view('/pages/product', compact('product'));
}
And my product.blade.php (nested in views/pages/product):
<img src="/images/product/Frozen_Ophelia_800x.png">
I keep getting a ReflectionException Class App\Product does not exist.
I got this working when I just returned a view from the route. I'm getting a ReflectionException
Class App\Product does not exist
so I think it's something at the top, ie. use App\Product;
that is wrong.
Edit (below is my App\Product
nested in app/Providers
):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
//
use SoftDeletes
protected $fillable = [
'name', 'price', 'units', 'description', 'image'
];
public function orders(){
return $this->hasMany(Order::class);
}
}
Upvotes: 0
Views: 415
Reputation: 473
Assuming App\Product
model exists, correct code should be:
public function index() {
$product = Product::all();
return view('pages.product', compact('product'));
}
Check the docs.
PS did you call a $ composer dumpautoload
? ReflectionException Class
error is often related to new class autoloading (eg. new classes in a packages)
Upvotes: 2
Reputation: 34914
view function should have any view template not any url or route. Of you have file views/pages/product.blade.php
then use
view('pages.product',compact('product'));
Upvotes: 1