Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “我怎样才能解决问题?”;试图获取非对象的属性“;_Php_Laravel_Laravel 5_Relational Database_Relationship - Fatal编程技术网

Php “我怎样才能解决问题?”;试图获取非对象的属性“;

Php “我怎样才能解决问题?”;试图获取非对象的属性“;,php,laravel,laravel-5,relational-database,relationship,Php,Laravel,Laravel 5,Relational Database,Relationship,我在我的网站上有一个用户最喜欢的列表,他们可以将他们最喜欢的房子添加到愿望列表中 进展顺利,但他无法看到愿望列表页面,错误如下: optional($fav->house)->title 正在尝试获取非对象的属性“image” 这是我的亲戚 class Home extends Model { protected $guarded = []; public function favorite() { return $this->hasMany(favor

我在我的网站上有一个用户最喜欢的列表,他们可以将他们最喜欢的房子添加到愿望列表中

进展顺利,但他无法看到愿望列表页面,错误如下:

optional($fav->house)->title
正在尝试获取非对象的属性“image”

这是我的亲戚

class Home extends Model
{
  protected $guarded = [];
  public function favorite() 
  {
    return $this->hasMany(favorite::class,'house_id');
  }
}

class favorite extends Model
{
  protected $guarded = [];
  public function house()
  {
    return $this->belongsTo(home::class);
  }
}
控制器中的我的索引函数:

public function index()
{
   $favorite = favorite::where('user_id',auth()->user()->id)->get();
   return view('favorite.index',compact('favorite'));
}
我的索引:

@foreach($favorite as $fav)

  <tr>
    <td>
      <a href="property-detail.html"><img src="{{$fav->home->image}}" alt=""
                                                                            width="100"></a>
      </td>
      <td><a href="property-detail.html">{{$fav->home->title}}</a></td>
      <td>خانه خانواده</td>
      <td>اجاره</td>
      <td>
        <div class="price"><span>{{number_format($fav->home->price)}}</span><strong>تومان</strong>
        </div>
      </td>
      <td>
        <a href="#" class="action-button"><i class="fa fa-ban"></i> <span>حذف</span></a>
      </td>
    </tr>
  @endforeach
@foreach($fav的收藏夹)
خانه خانواده
اجاره
{{number_格式($fav->home->price)}
@endforeach

你最喜欢的模型没有家庭关系的第一件事就是你的关系是房子,当你想得到它的值时,你可以使用如下的
可选的帮助函数:

optional($fav->house)->title

你最喜欢的模型没有家庭关系的第一件事是你的关系是房子,当你想得到它的值时,你可以使用
可选的
助手函数,如下所示:

optional($fav->house)->title

您试图使用错误的名称访问关系。您定义了与name
house
的关系:

public function house(){

    return $this->belongsTo(home::class);
}
因此,您需要使用此名称来访问:

@foreach($favorite as $fav)
    <tr>
        <td>
            <a href="property-detail.html"><img src="{{$fav->house->image}}" alt="" width="100"></a>
        </td>
        <td><a href="property-detail.html">{{$fav->house->title}}</a></td>
        <td>خانه خانواده</td>
        <td>اجاره</td>
        <td>
            <div class="price"><span>{{number_format($fav->house->price)}}</span><strong>تومان</strong>
            </div>
        </td>
        <td>
            <a href="#" class="action-button"><i class="fa fa-ban"></i> <span>حذف</span></a>
        </td>
    </tr>
@endforeach

您试图使用错误的名称访问关系。您定义了与name
house
的关系:

public function house(){

    return $this->belongsTo(home::class);
}
因此,您需要使用此名称来访问:

@foreach($favorite as $fav)
    <tr>
        <td>
            <a href="property-detail.html"><img src="{{$fav->house->image}}" alt="" width="100"></a>
        </td>
        <td><a href="property-detail.html">{{$fav->house->title}}</a></td>
        <td>خانه خانواده</td>
        <td>اجاره</td>
        <td>
            <div class="price"><span>{{number_format($fav->house->price)}}</span><strong>تومان</strong>
            </div>
        </td>
        <td>
            <a href="#" class="action-button"><i class="fa fa-ban"></i> <span>حذف</span></a>
        </td>
    </tr>
@endforeach

您的所有用户都有此问题,还是只有1个?因为如果只有一个用户存在此问题,则需要验证数据库中与此用户的收藏夹关联的所有数据是否正确。如果每个用户都有这个问题,那么您需要测试关系。对于后者,您可以使用
php artisan tinker
并使用类似
App\favorite::whereUserId($someID)->first()->house()->get()
的代码,其中
$someID
是一个用户id。目标是查看您是否得到了有效的结果。您的所有用户都有这个问题,还是只有1个?因为如果只有一个用户存在此问题,则需要验证数据库中与此用户的收藏夹关联的所有数据是否正确。如果每个用户都有这个问题,那么您需要测试关系。对于后者,您可以使用
php artisan tinker
并使用类似
App\favorite::whereUserId($someID)->first()->house()->get()的代码
其中,
$someID
是一个用户id。目标是查看您是否获得了有效的结果。此外,您没有向房屋添加数据以将其与收藏夹模型关联,也没有向房屋添加数据以将其与收藏夹模型关联