我正在调整一个部分用laravel编程的mp3播放器。由于我是这门语言的新手,我想请求一些帮助。对此有任何意见,我们非常感谢!
MP3播放器提供了处理多种类型的功能。在每种类型中都存在许多歌曲。每首歌曲都可以投票或投票(或不投票)。访问者只能在每首歌曲上投票或下注。
对于这段代码,两个表很重要:歌曲和动作。请找到两个截图。
歌曲表1
动作表2
我希望显示启用的rondom歌曲(桌面歌曲),并且唯一用户没有投票(不喜欢),换句话说用户没有对该歌曲进行投票(未记录在表格中)或赞成歌曲(赞)。
为了实现这一点,我正在努力解决// ---之间的代码问题。所有其他代码都正常工作。
<?php
namespace App\Http\Controllers;
use App\Genre;
use App\Song;
use App\Action;
use Illuminate\Http\Request;
use App\Http\Requests;
class HomeController extends Controller
{
public function index(Request $request){
if(isset($_COOKIE['authuser'])){
$authuser = $_COOKIE['authuser'];
}else{
$authuser = 'u'.uniqid();
setcookie('authuser', $authuser, time() + (10 * 365 * 24 * 60 * 60));
}
$selectedGenre = Genre::first();
//---
$song = $selectedGenre->songs()->where('isEnabled',1);
$action = $song->actions()->where('name', 'Like')->where('authuser', $authuser);
->inRandomOrder()->first()
//---
$song->playcount++;
$song->save();
$genres = Genre::orderBy('priority', 'ASC')->get();
return view('index', compact(['song', 'genres', 'selectedGenre', 'authuser']));
}
再一次,任何帮助都非常感谢! :)
答案 0 :(得分:0)
试试这个:
$randomSong = $selectedGenre->songs()->where('isEnabled',1)
->whereDoesntHave('actions', function ($q) use ($authUser) {
$q->where('authUser', $authUser);
})->inRandomOrder()->first();
哦和建议,不要使用$ _变量。总是更好地使用Laravel的包装器,例如$request->cookie('name')