Support the ongoing development of Laravel.io →
Database Eloquent Blade

Hello,

I currently have a page that lists all of the 'products' in my database by name. I am trying to make it so that when I click on a product name, it goes to a new 'productDetails' page that gives it's info info using the primary key(id) of the product models.

I currently have my routes as:

Route::get('productDetails/{id}', function(App\productDetails $id){ $productId = productDetails::find($id); return view('productDetails'. $productId); }); Route::get('productDetails', function(){ return view('productDetails'); });

My Controller as:

class productDetailsController extends Controller{

public function single($id) {
    $products= productDetails::all();
    $productId = productDetails::find($id);
  
   return $productId;

}
public function boot(Router $router)
{
  parent::boot($router);

  $router->model('id', 'App\Product');
}

}

And the model as: class productDetails extends \Illuminate\Database\Eloquent\Model {

/**
 *The attributes that are mass assignable. 
 * 
 * @var array 
 */
protected $fillable = ['name', 'price', 'description'];

/**
 *The database table used by the model 
 * 
 * @var string 
 */
protected $table = 'products';

/**
 *Timestamp attribute not required
 * 
 * @var boolean
 */
public $timestamps = false;

/**
 *Attributes that are not mass assignable
 * 
 * @var array 
 */
protected $guarded = ['id']; 

}

The webpages are there but when I try to output the info, it says the variables are undefined.

Can anyone please help me out on this?

Last updated 3 years ago.
0

you need to pass the data to the view: http://laravel.com/docs/5.0/views

0

I thought that was what I was doing in the route: Route::get('productDetails/{id}', function(App\productDetails $id){ $productId = productDetails::find($id); return view('productDetails'. $productId); });

but when I try to use $productId in the blade, it says it's undefined.

0

your productDetails should be a class that extends Eloquent Model.. Since you are using type hint, you can just replace $id with $productDetails perhaps, do not type hint your $id with the productDetails if you are passing in an interger id then use find() method. Its either you use Model Type hinting and directly use the argument that is being typehinted then pass it to the view OR just use $id argument without typehint and use find() method.

Last updated 10 years ago.
0

Also, looking at your routes, you did not call any controller.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

sbd12b sbd12b Joined 26 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.