第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何限制php中的問題?

如何限制php中的問題?

PHP
qq_笑_17 2022-07-22 15:27:46
我使用flutter和php,我只需要從數(shù)據(jù)庫中獲取40個(gè)隨機(jī)問題,但我是php的初學(xué)者!我該怎么做?見下面的代碼public function index(){$user = Auth::user();$topics = Topic::all()->toArray();$questions = Question::all()->toArray();    return response()->json(array('users' =>$user,'topics'=>$topics,'questions'=>$questions), 200);// return response()->json(['data' => $posts], 200, [], JSON_NUMERIC_CHECK);}public function show_question($id){    $topic = Topic::findOrFail($id);    $user_id = Auth::user()->id;    $existing = Answer::where('user_id',$user_id)->where('topic_id',$id)->first();    if($existing == ""){        $topic_ques = Question::where('topic_id', $id)->get()->shuffle();         $questions = $topic_ques->only(40);        return response()->json(array('topic'=>$topic,'questions' => $questions), 200);    }    else{      return response()->json("test already given", 300);      }}
查看完整描述

3 回答

?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

public function index(){


$user = Auth::user();

$topics = Topic::all()->toArray();

$questions = Question::all()->random(40)->toArray();


    return response()->json(array('users' =>$user,'topics'=>$topics,'questions'=>$questions), 200);


// return response()->json(['data' => $posts], 200, [], JSON_NUMERIC_CHECK);

}


public function show_question($id){

    $topic = Topic::findOrFail($id);

    $user_id = Auth::user()->id;

    $existing = Answer::where('user_id',$user_id)->where('topic_id',$id)->first();

    if($existing == ""){

        $topic_ques = Question::where('topic_id', $id)->get()->shuffle(); 

        $questions = $topic_ques->only(40);

        return response()->json(array('topic'=>$topic,'questions' => $questions), 200);

    }

    else{

      return response()->json("test already given", 300);  

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-07-22
?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊

嘗試這個(gè):

$questions = Question::where('topic_id', $id)->inRandomOrder()->take(40)->get();

有關(guān)更多信息,請(qǐng)參閱Laravel 文檔


查看完整回答
反對(duì) 回復(fù) 2022-07-22
?
蕪湖不蕪

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊

 $topic_ques = Question::where('topic_id', $id)->limit(40)->get()->shuffle();

只需使用限制功能。


查看完整回答
反對(duì) 回復(fù) 2022-07-22
  • 3 回答
  • 0 關(guān)注
  • 149 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)