Learn Android with Java and Kotlin

The Controllers: Retrieving a particular item from the database

Intro: The home page of our site is displaying the list of products from the database now, but the details page is still showing the static data. Now we shall start displaying actual data from the database here, but we won’t be able to complete this in this video itself… why? You’ll know by the end of this video. So let’s get started..

The process is similar to what we did in while retrieving the list of products-

  1. Create a function inside ProductsController
  2. Fetch a product by id
  3. Update the route to invoke this function
  4. Display the fetched item in the product view (the product details page)


Open the ProductsController

Create a new function, let us call it show(), we shall add a parameter for the id of the product.
public function show($id) { }

Call the function find() of the Product model, pass the ID to it. This function fetches the data of a row from the table associated with the Model, by the ID passed to it, and it returns an instance of the Model holding the fetched data. In this case the table associated with our product model is the products table, so this function will fetch a particular product by the id passed to it from the products table. And it will return an instance of the Product model holding the fetched data. So let us create a variable named $product to hold the result.
public function show($id) { $product=Product::find($id); }

Add a return statement, call the function view() to return the product view. And we shall pass the variable $product to the view using the function with().
public function show($id) {
return view('product')->with("product",$product);

Now we have to make some changes to the route for the product details page. Replace the closure with a call to the function show() of the product controller. Route::get(‘/product/{id}’,[ProductsController::class,’show’]); If you are wondering about passing the id from the URI to the show() function, then don’t worry…. Laravel will take care of that, it will make the id available to us in the show() function.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductsController;
| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/dashboard', function () {
    return view('dashboard');

Route::get('/',[ProductsController::class,'index'] );

  return view('sell');


require __DIR__.'/auth.php';

Now open the product view (the file product.blade.php), and replace the static strings with the content from the variable $product that we have passed to this view. Use {{asset($product->image_url)}} in the img element.
Use {{$product->title}} for the product title.
Use {{$product->short_desc}} for the product short description.
Use {{$product->long_desc}} for the long description and
use {{$product->price}} for the product price.

  <!-- Main content -->
      <div class="flex m-4">
        <!-- Left half -->
        <div class="w-1/2 rounded overflow-hidden shadow">
          <img class="object-cover w-full" src="{{asset($product->image_url)}}"/>
        <!-- Right half -->
        <div class="w-1/2 rounded bg-white ml-2 p-6 shadow relative">

            <div class="font-semibold">{{$product->title}}</div>
            <div class="text-sm text-gray-500">{{$product->short_desc}}</div>
            <div class="text-xs text-gray-500 mt-2">{{$product->long_desc}}</div>

            <!-- Seller Information -->
            <div class="mt-4">
              <div class="text-xs text-gray font-semibold">Sold by:</div>
              <div class="text-sm text-gray-500">John Doe</div>
            <div class="mt-2">
              <div class="text-xs text-gray font-semibold">Phone number:</div>
                <div class="text-sm text-gray-500">012356789</div>
                <div class="text-sm text-gray-500">**********<a href="/login" class="text-xs text-blue-500">(Login to view)</a></div>
            <div class="mt-2">
              <div class="text-xs text-gray font-semibold">Email address:</div>
                <div class="text-sm text-gray-500">abc@example.com</div>
                <div class="text-sm text-gray-500">**********<a href="/login" class="text-xs text-blue-500">(Login to view)</a></div>

            <!-- Product Price -->
            <div class="absolute bottom-0 right-0 bg-green-500 px-4 py-2 m-6 rounded-full">
              <div class="text-white font-bold text-sm">${{$product->price}}</div>


But, we also have to display the contact information of the seller.

How to get the contact details of the user? For this, we have to write some code inside the Product model, we shall do that.. but first, let’s handle the other issue. That is.. we need to display the mobile number here, but we are not yet collecting it. So we shall ask the user to enter the mobile number while signing up.

How to start collecting mobile numbers on registration? We have to make some changes to the registration form and to some of the related files, I’ll show you each of the steps in the next video.

Leave a Reply

Your email address will not be published.