亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在使用 laravel 7 獲得最高票數的候選人和他的名字時遇到問題

在使用 laravel 7 獲得最高票數的候選人和他的名字時遇到問題

PHP
慕的地8271018 2023-03-11 14:04:13
我正試圖從得票最高的候選人那里得到他的名字,所有這些都試過失敗。在獲得所有總統候選人之后,我只是在下面添加()。Illuminate\Database\Eloquent\Collection {#288 ▼  #items: array:4 [▼    0 => App\Candidate {#289 ▼      #connection: "mysql"      #table: "candidates"      #primaryKey: "id"      #keyType: "int"      #attributes: array:9 [▼        "id" => 1        "name" => "Brian"        "seat" => "president"        "regno" => "DIT-C004-0536\2013"        "votes" => 0     // this is the first candidate record, others are below.        "user_id" => 2        "created_at" => "2017-05-12 08:43:58"        "updated_at" => "2017-05-12 08:43:58"        "image" => ""      ]    }    1 => App\Candidate {#290 ▼      #attributes: array:9 [▼        "id" => 5        "name" => "Juma"        "seat" => "president"        "regno" => "DIT-C004-0516\2013"        "votes" => 3        "user_id" => 7        "created_at" => "2017-05-12 09:03:03"        "updated_at" => "2017-05-12 14:48:54"        "image" => ""      ]    }    2 => App\Candidate {#291 ▼      #attributes: array:9 [▼        "id" => 9        "name" => "Stephen"        "seat" => "president"        "regno" => "0001"        "votes" => 4           //trying to get the highest votes with the name.        "user_id" => 12        "created_at" => "2020-03-17 15:27:47"        "updated_at" => "2020-03-21 19:18:39"        "image" => ""      ]    }    3 => App\Candidate {#292 ▼      #attributes: array:9 [▼        "id" => 10        "name" => "Gobby"        "seat" => "president"        "regno" => "DIT-C004-0436\2014"        "votes" => 0        "user_id" => 11        "created_at" => "2020-03-17 15:27:59"        "updated_at" => "2020-03-17 15:27:59"        "image" => ""      ]     }我一直在四處尋找解決方案,但找不到,在此先感謝。我在循環之前轉儲了代碼,因為當我循環時它一直給我 0 或者有時告訴我數組不能轉換為 int 之類的東西。
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

在這種情況下,您的控制器代碼應該是這樣的:


$seat = 'president';

$name= 'gobby';

$data = Candidate::where('seat', $seat)

                  ->where('name', $name)

                  ->orderBy('votes','desc')

                  ->first()

print_r($data);

它將為您提供席位為“總統”和名稱為“蝦虎魚”的最大選票數據


查看完整回答
反對 回復 2023-03-11
  • 1 回答
  • 0 關注
  • 137 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號