Ashar Khan Ashar Khan -3 years ago 96
MySQL Question

getting undefined variable error in laravel

i have two model Hotel and Comment and their respective migration inside hotel there is a function
**

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Hotel extends Model
{
public function Comments(){
return $this->hasMany(Comment::class);
}
}


** and Comment Model is
**

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function hotel(){
return $this->belongsTo(hotel::class);
}
}


**
When i am using tinker i get the correct result but when i use {{$hotel->name}} to show the name of the hotel present in table it will show an error undefined variable .
error

(2/2) ErrorException

Undefined variable: hotel (View: /var/www/html/tpfl/resources/views/welcome.blade.php)

Answer Source

Make sure $hotel is an initialized object by doing this:

$hotel = new hotel();
$hotel->name;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download