1

I can't find how to print data on the blade.view. I have a list of "customers" on overview.blade.php, and I have a button that redirects to their profile based on their "nCustomerID" (which is blade.view for me).

When I use the dump function on blade.view page does show the correct data for each " customer" I have in my database, but I don't know how to print it on the blade.view. Would love to get some documentation or explanation to read along on this.

Controller:

    public function show($id)
    {
        $oCustomer = Customer::getCustomer($id);
        return view('Customers.view', ['nCustomerID' => $id]);
    }
Moshiur
  • 659
  • 6
  • 22
Tim
  • 47
  • 1
  • 1
  • 10
  • why are you passing `$id` to view? you should pass `$oCustomer` then print it on view – Moshiur Feb 04 '20 at 08:33
  • @Moshiur I'm kinda new to laravel and this is the way I figured out it worked (not as it's suppose to as it seems). I'll edit it. – Tim Feb 04 '20 at 08:38

4 Answers4

1

Using Compact() methods to show data in Laravel blade view.

public function show($id)
{
    $oCustomer = Customer::getCustomer($id);
    return view('Customers.view',compact('oCustomer');
}

In view try this way.

 /*if oCustomer is a collection */
 @foreach($oCustomer as $customer)
     {{$customer->name}}
 @endforeach

 /* if oCustomer is a single object*/
 {{$oCustomer->name}} or {{$oCustomer[0]->name}}
Arun J
  • 687
  • 4
  • 14
  • 27
Gabrielle-M
  • 1,037
  • 4
  • 17
  • 39
  • Thanks, this helped me a bit. Any chance you could help me the other data (such as name)? or give me a push in the right direction? @Gabrielle-M – Tim Feb 04 '20 at 08:33
1

use below code to print id

{{ $nCustomerID }}
Noman Shaikh
  • 139
  • 2
  • 14
0

Use compact() in controller to pass a variable or data into blade file.

public function show($id)
{
    $oCustomer = Customer::getCustomer($id);
    return view('Customers.view', compact(['oCustomer','id]));
}

In blade

{{$oCustomer}}
{{$id}}

For Details https://laravel.com/docs/6.x/blade

Arun J
  • 687
  • 4
  • 14
  • 27
albus_severus
  • 3,626
  • 1
  • 13
  • 25
  • Alright, I tried this but now I get this error: htmlspecialchars() expects parameter 1 to be string, object given (View: Path\view.blade.php). Any way I could print as object instead? – Tim Feb 04 '20 at 08:44
  • https://stackoverflow.com/questions/43217872/laravel-htmlspecialchars-expects-parameter-1-to-be-string-object-given follow this – albus_severus Feb 04 '20 at 08:54
0

you can also pass variables using array in controller

public function show($id)
{
   $oCustomer = Customer::getCustomer($id);
   return view('Customers.view',['data'=>$oCustomer]);
}

and in you view file

{{$data}}
Khusal Berva
  • 114
  • 5