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?
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.
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community