【问题标题】:Trying to get records from database and pass to controller试图从数据库中获取记录并传递给控制器
【发布时间】:2020-04-12 16:58:55
【问题描述】:

我正在尝试在第二个用户在游戏中出价后向在游戏中第一个出价的用户发送通知。

我有表格 game_bids,其中有 id、user_id 和 game_id。 我试图从表中找到与当前打开的游戏相同的 game_id。

我不擅长 laravel,不知道如何让它正常工作。我需要 $user = 的帮助(我将通过它来通知)。

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Game_bid;
use App\Game;
use Auth;
use Illuminate\Support\Facades\Lang;
use DB;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Notification;
use App\Notifications\GameBiddedNotification;


class PointsController extends Controller {
public function bid($game_id) {
        $bid = new Game_bid;
        $bid->game_id = $game_id;
        $bid->user_id = Auth::user()->id;
        $bid->is_awarded = 0;

        if ($bid->save()) {
            $game = Game::find($game_id);
            $user = User::find(Auth::user()->id);
            $user->points = $user->points - $game->points;
            $user->save();
        }

        **$user = Game_bid::where('user_id')
                ->where('game_id', Game::find($game_id))
                ->first();**

        $details = [
                'greeting' => $game->title,
                'body' => '.',
                'thanks' => '!',
        ];

        $user->notify(new \App\Notifications\GameBiddedNotification($details));

        return redirect('my_bids')->with('success', 'Your bid placed successfully');
    }

【问题讨论】:

    标签: database laravel eloquent


    【解决方案1】:

    如果我理解正确,可能会有多个用户需要通知(所有出价过此游戏的人)。因此,要检索并通知所有其他对该游戏出价的用户,您可以执行以下操作:

    public function bid($game_id) {
        $bid = new Game_bid;
        $bid->game_id = $game_id;
        $bid->user_id = Auth::user()->id;
        $bid->is_awarded = 0;
    
        if ($bid->save()) {
            $game = Game::find($game_id);
            $user = User::find(Auth::user()->id);
            $user->points = $user->points - $game->points;
            $user->save();
        }
    
        $users = User::whereHas('game_bids', function ($query) use ($game_id, $bid) {
            $query->where('game_id', $game_id)->where('user_id', '!=', $bid->user_id);
        })->get();
    
        $details = [
            'greeting' => $game->title,
            'body' => '.',
            'thanks' => '!',
        ];
    
        foreach ($users as $user) {
            $user->notify(new \App\Notifications\GameBiddedNotification($details));
        }
    
        return redirect('my_bids')->with('success', 'Your bid placed successfully');
    }
    

    【讨论】:

    • 感谢您的回答。是的,可以有不止一个用户出价,最重要的是,一旦有更多用户出价,第一个出价的用户应该会收到通知。我收到此错误:BadMethodCallException 调用未定义的方法 App\User::game_bids()
    • 您必须将 'game_bids' 更改为您定义与 game_bids 的关系的任何内容,检查您的用户模型,它应该如下所示:public function game_bids() { return $this->hasMany('App\Game_bid') } 然后将 'game_bids 替换为该函数名称。
    • 对不起,我想你复制粘贴了代码,我在$query 上更改第二个where() 条件时不小心漏掉了一个> 字符,我更新了答案。
    • 很高兴为您提供帮助! ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2019-11-01
    • 1970-01-01
    • 2019-11-11
    • 2021-12-26
    • 1970-01-01
    • 2017-12-23
    相关资源
    最近更新 更多