I’m working with Laravel 8 to develop my project and in this project, I have a table named hostings
which it’s migration goes here:
public function up() { Schema::create('hostings', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); }
And now I want to return some results from the DB and show them on blade, so I added this:
<table> <tr> <th>Hosting Name:</th> <td>{{ $hosting->name }}</td> </tr> </table>
But now I get this error:
Property [name] does not exist on this collection instance
So what is going wrong here? How can I fix this issue?
I would really appreciate if you share any idea or suggestion about this with me,
Thanks in advance.
Controller code:
public function index() { $hosting = Hosting::all(); return view('admin.index', compact('hosting')); }
Advertisement
Answer
as you have collection you need to loop over it to get value
<table> <tr> @foreach ($hosting as $host) <th>Hosting Name:</th> <td>{{ $host->name }}</td> @endforeach </tr> </table>
else you can show all the host name in comma separated by this
<table> <tr> <th>Hosting Name:</th> <td>{{ $hosting->pluck('name')->join(',') }}</td> </tr> </table>
or you can get first data of collection like this
<table> <tr> <th>Hosting Name:</th> <td>{{ $hosting->first()->name }}</td> </tr> </table>
ref link https://laravel.com/docs/8.x/collections#method-pluck