Php laravel 5.4未定义的属性:stdClass::$id

Php laravel 5.4未定义的属性:stdClass::$id,php,laravel-5.4,Php,Laravel 5.4,我有两个模特艺术家和两首歌,它们有一对多的关系,一个艺术家可以有多首歌。这首歌属于一位艺术家 $artists = DB::table('artists')->join('songs', 'artists.id', '=', 'songs.artist_id')->select('artists.*, songs.id, songs.title, songs.artist_id, songs.week_hits, songs.created_at') ->

我有两个模特艺术家和两首歌,它们有一对多的关系,一个艺术家可以有多首歌。这首歌属于一位艺术家

$artists = DB::table('artists')->join('songs', 'artists.id', '=', 'songs.artist_id')->select('artists.*, songs.id, songs.title, songs.artist_id, songs.week_hits, songs.created_at')
            ->SELECT(DB::raw('sum(songs.week_hits) as WEEKHITS'))->groupBy('songs.artist_id')->orderBy('WEEKHITS', 'DESC')->take(7)->get();
 return return view('front.home', compact('videos', 'songs', 'artists', 'played', 'uploads', 'albums'));
这是我的控制器代码。如你所见,我添加了所有艺人歌曲周点击率,并按歌曲进行分组。艺人id

这是我的观点

 @foreach($artists as $artist)
            <div class="artist-grid">
                <div class="art-img">
                    <a href="{{route('artist',['id' => $artist->id, 'slug' => $artist->slug])}}">
                        <img  src="image/small/{{$artist->image}}">
                    </a>
                </div>
                <div class="name">
                    <h3>{{$artist->name}}</h3>
                </div>
            </div>
             @endforeach
但当我添加($artists)时,这就是输出

Collection {#293 ▼
  #items: array:4 [▼
    0 => {#307 ▼
      +"WEEKHITS": "39"
    }
    1 => {#292 ▼
      +"WEEKHITS": "9"
    }
    2 => {#278 ▼
      +"WEEKHITS": "4"
    }
    3 => {#308 ▼
      +"WEEKHITS": "1"
    }
  ]
}

非常感谢您的帮助。

请按如下方式尝试返回视图:

return view('front.home')->with($artists, compact('artists'));
编辑 要传递多个变量,有许多解决方案。比如:

return view('front.home')->with(compact('videos', 'songs', 'artists', 'played', 'uploads', 'albums'));

请查看您的查询,您还必须在查询的select()选项中选择id

$artists = DB::table('artists')->join('songs', 'artists.id', '=', 'songs.artist_id')->select('artists.*, songs.id, songs.title, songs.artist_id, songs.week_hits, songs.created_at')
            ->SELECT(DB::raw('sum(songs.week_hits) as WEEKHITS','artists.id as id'))->groupBy('songs.artist_id')->orderBy('WEEKHITS', 'DESC')->take(7)->get();


问题似乎出在您的查询中,您选择了两次,第二个select语句覆盖了第一个语句,这就是为什么您只得到“WEEKHITS”
组合两个select语句。

很抱歉,我没有在上面提到,我不仅发送$artists变量以查看,还有另一个视图,返回视图('front.home',compact('videos','songs','artists','played','uploads','albums');您可以使用多个
发送多个变量。查看我更新的naswer@mahamoudmahamedyou dd()美元美术师收藏值,并向我们显示单个输入的结果吗?这是我dd($美术师)变量的时候。收藏{#293▼   #项目:阵列:4[▼     0 => {#307 ▼       +“WEEKHITS”:“39”}1=>{#292▼       +“WEEKHITS”:“9”}2=>{#278▼       +“WEEKHITS”:“4”}3=>{308▼       +“WEEKHITS”:“1”}]}除了他们,我仍然没有做任何改变。我认为问题在于这一部分。由于您可以在foreach()中找到id route$artist->idplease dd()的属性,因此您可以看到$artist对象的实际情况。
$artists = DB::table('artists')->join('songs', 'artists.id', '=', 'songs.artist_id')->select('artists.*, songs.id, songs.title, songs.artist_id, songs.week_hits, songs.created_at')
            ->SELECT(DB::raw('sum(songs.week_hits) as WEEKHITS','artists.id as id'))->groupBy('songs.artist_id')->orderBy('WEEKHITS', 'DESC')->take(7)->get();
  $artists = DB::table('artists')->join('songs', 'artists.id', '=', 'songs.artist_id')->select('artists.*, songs.id, songs.title, songs.artist_id, songs.week_hits, songs.created_at')
                ->SELECT(DB::raw('sum(songs.week_hits) as WEEKHITS','artists.*'))->groupBy('songs.artist_id')->orderBy('WEEKHITS', 'DESC')->take(7)->get();