【问题标题】:getting undefined variable error in laravel在laravel中得到未定义的变量错误
【发布时间】:2017-11-15 20:20:56
【问题描述】:

我有两个模型:HotelComment,连同它们各自在酒店内部的迁移有一个功能:

酒店模型:

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

评论模型:

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

当我使用 tinker 时,我得到了正确的结果,但是当我使用 {{$hotel-&gt;name}} 显示表中存在的酒店名称时,它会显示错误 Undefined variable

错误:

(2/2) ErrorException

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

【问题讨论】:

  • 如何从控制器传递给视图?
  • 您是否将 $hotel 变量从控制器传递到视图?

标签: php mysql laravel model migration


【解决方案1】:

通过这样做确保$hotel 是一个初始化对象:

$hotel = new hotel();
$hotel->name;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2018-12-18
    相关资源
    最近更新 更多