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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?