无法将第二个 table 数据传递给 laravel 中的 foreach
Can't pass through second table data to foreach in laravel
我正在构建一个大学项目,我无法传递第二个 table 与主要 table / class 的数据。
当我尝试传递它并在视图中访问它时,出现尝试访问非对象错误。
下面是我的代码。
健身房class
class Gym extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
*/
protected $table = 'gyms';
public $gym;
public function reviews()
{
return $this->hasMany('Review', 'unique_gym_id'); // represents the Review class
}
页面控制器相关部分,我将数据从这个循环传递到视图。
foreach($gymsearchs as $gymsearch)
{
Gym::save($gymsearch);
$gyms[] = DB::table('gyms')->where('unique_gym_id', '=', $gymsearch->unique_gym_id)->get();
Review::review($gymsearch);
$reviews[] = DB::table('reviews')->where('unique_gym_id', '=', $gymsearch->unique_gym_id)->get();
}
//dd($reviews); this shows the full review object contents fine
$data = array_add($data, 'gyms', $gyms, 'reviews', $reviews);
查看
@foreach(array_slice($gyms, 0, 5) as $gym)
{{$gym->name}} // works fine and with other objects from the $gym
@endforeach
@foreach(array_slice($gyms, 0, 5) as $gym)
{{$gym->review}} // this gives me a trying to access non object error.
@endforeach
健身房模型和评论模型都有 unique_gym_id 作为列
我认为健身房模型中的这篇评论 class 应该通过 $gym 的评论 table 数据?
public function reviews()
{
return $this->hasMany('Review', 'place_id');
}
知道我遗漏了什么吗?谢谢我是 laravel 和 php
的新手
你告诉它是一个 hasMany 关系,所以它不应该是“{{$gym->reviews}}”吗?没试过,但看起来很奇怪。此外,这应该 return 一个数组而不是单个对象,因此您必须对其进行迭代。
编辑:
整个部分:
foreach($gymsearchs as $gymsearch)
{
Gym::save($gymsearch);
$gyms[] = DB::table('gyms')->where('unique_gym_id', '=', $gymsearch->unique_gym_id)->get();
Review::review($gymsearch);
$reviews[] = DB::table('reviews')->where('unique_gym_id', '=', $gymsearch->unique_gym_id)->get();
}
//dd($reviews); this shows the full review object contents fine
$data = array_add($data, 'gyms', $gyms, 'reviews', $reviews);
可能是
Gym::save($gymsearchs);
$gyms = Gym::where('unique_gym_id', 'in', array_map(create_function('$o', 'return $o->unique_gym_id;'), $gymsearchs))->get();
$data['gyms'] = $gyms;
,因为您似乎没有使用 $reviews。但是我不太了解 gymsearchs 的意图以及您尝试使用 Review::review($gymsearch) 方法做什么。您还应该研究这种情况下的预加载。有一个 ->with($relation) 函数,可以预先加载您的所有评论。这样您就可以优化发送到数据库的查询数量。
我正在构建一个大学项目,我无法传递第二个 table 与主要 table / class 的数据。
当我尝试传递它并在视图中访问它时,出现尝试访问非对象错误。
下面是我的代码。
健身房class
class Gym extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
*/
protected $table = 'gyms';
public $gym;
public function reviews()
{
return $this->hasMany('Review', 'unique_gym_id'); // represents the Review class
}
页面控制器相关部分,我将数据从这个循环传递到视图。
foreach($gymsearchs as $gymsearch)
{
Gym::save($gymsearch);
$gyms[] = DB::table('gyms')->where('unique_gym_id', '=', $gymsearch->unique_gym_id)->get();
Review::review($gymsearch);
$reviews[] = DB::table('reviews')->where('unique_gym_id', '=', $gymsearch->unique_gym_id)->get();
}
//dd($reviews); this shows the full review object contents fine
$data = array_add($data, 'gyms', $gyms, 'reviews', $reviews);
查看
@foreach(array_slice($gyms, 0, 5) as $gym)
{{$gym->name}} // works fine and with other objects from the $gym
@endforeach
@foreach(array_slice($gyms, 0, 5) as $gym)
{{$gym->review}} // this gives me a trying to access non object error.
@endforeach
健身房模型和评论模型都有 unique_gym_id 作为列
我认为健身房模型中的这篇评论 class 应该通过 $gym 的评论 table 数据?
public function reviews()
{
return $this->hasMany('Review', 'place_id');
}
知道我遗漏了什么吗?谢谢我是 laravel 和 php
的新手你告诉它是一个 hasMany 关系,所以它不应该是“{{$gym->reviews}}”吗?没试过,但看起来很奇怪。此外,这应该 return 一个数组而不是单个对象,因此您必须对其进行迭代。
编辑:
整个部分:
foreach($gymsearchs as $gymsearch)
{
Gym::save($gymsearch);
$gyms[] = DB::table('gyms')->where('unique_gym_id', '=', $gymsearch->unique_gym_id)->get();
Review::review($gymsearch);
$reviews[] = DB::table('reviews')->where('unique_gym_id', '=', $gymsearch->unique_gym_id)->get();
}
//dd($reviews); this shows the full review object contents fine
$data = array_add($data, 'gyms', $gyms, 'reviews', $reviews);
可能是
Gym::save($gymsearchs);
$gyms = Gym::where('unique_gym_id', 'in', array_map(create_function('$o', 'return $o->unique_gym_id;'), $gymsearchs))->get();
$data['gyms'] = $gyms;
,因为您似乎没有使用 $reviews。但是我不太了解 gymsearchs 的意图以及您尝试使用 Review::review($gymsearch) 方法做什么。您还应该研究这种情况下的预加载。有一个 ->with($relation) 函数,可以预先加载您的所有评论。这样您就可以优化发送到数据库的查询数量。