dtvpe4837413 2016-02-21 03:59
浏览 117
已采纳

Laravel 5.2使用动态内容从select中插入

As a novice to laravel I am hoping I can get to the bottom of this rather irritating issue!

I have a small app that consists of 2 user types, Buyers and Providers, The usertype->buyers can request a free estimation for a service they require and the provider can create this manually or have an estimation sent automatically based on what they set as there default value for that specific price range so for example if a user(buyer) requests an estimation for a service that is £200 the provider may have set a default estimation of £180 - £220 for that price range and this is what will be shown to the user.

The problem I have is to make it fair we use the random function within laravel to randomly select 5 providers from the providers_table along with their default estimation and show this to the user from orderBy in descending order and looping through the results in the view.

$projectbids = ProjectBids::where('provider_id', '=', $proid)
    ->where('category_id', '=', $catid)
    ->orderBy('bid_price', 'desc')
    ->get()->random(5);

foreach($projectbids as $bid)
{{ $bid->bid_price }}
endforeach

So I'm trying to find a way to insert the 5 displayed estimations into a saved_estimates table so if the user refreshes the page or comes back to the page the model and view will not reorder the providers results based from the random(5) function that gets called.

I'm aware of this question: Create a Insert... Select statement in Laravel but wasn't to sure if there could be a better or easier way of doing this as my problem lays where I'm looping through the 5 results provided to the view from the table.

In a nutshell: I want to be able to store the 5 dynamic results straight into a saved_estimations table with several columns such as project_id, provider_id, user_id, estimated_price so the user can come back to their saved estimations in the future without them changing.

Very grateful for any input and advice, it will be very helpful to see what the more experienced users would suggest.

Thanks

-- Updated personal approach to see if there is room for improvement and to make it more laravel friendly.

    $results = $bidders;
    foreach($results as $row){
        $project = $projectid;
        $category = $row->category_id;
        $provider = $row->service_providers_id;
        $bidprice = $row->bid_price;
        $propvalue = $row->property_value_id;

        DB::table('static_bids')->insert(array(
            'project_id' => $project,
            'category_id' => $category,
            'service_providers_id' => $provider,
            'bid_price' => $bidprice,
            'property_value_id' => $propvalue
        ));
      }
  • 写回答

1条回答 默认 最新

  • duanjue6575 2016-02-21 04:42
    关注

    All you need is to use simple insertions into the table, something like this:

    foreach ($projectbids as $bid) { $result = SavedEstimation::create(array( 'project_id' => $bid->project_id, 'provider_id'=> $bid->provider_id, 'user_id' => $bid->user_id, 'estimated_price' => $bid->estimated_price )); }

    There is no way to insert this by using just one query anyway. You could simply store output HTML if the only reason you want to do this is to show same HTML to the user later, but this is bad practice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料