doww38701 2017-08-17 08:31
浏览 104
已采纳

数组转换为laravel中的字符串

I have been struggling with trying to change the output from my database query in php laravel framework from array to string. Below is my view code

<div class="switch-field">
<h5>Academic Level</h5>
<input class="changedVal" type="radio" id="highschool" name="academic_level" value="Highschool" />
<label for="highschool">High School</label>
<input class="changedVal" type="radio" id="college" name="academic_level" value="College" />
<label for="college">College</label>
<input class="changedVal" type="radio" id="university" name="academic_level" value="University" />
<label for="university">University</label>
<input class="changedVal" type="radio" id="masters" name="academic_level" value="Masters" />
<label for="masters">Master's</label>
<input class="changedVal" type="radio" id="phd" name="academic_level" value="Phd" />
<label for="phd">PhD</label>
</div>

<div class="switch-field">
<h5>Spacing</h5>
<input class="changedVal" type="radio" id="double_spaced" name="spacing" value="Double Spaced" />
<label for="double_spaced">Double Spaced</label>
<input class="changedVal" type="radio" id="single_spaced" name="spacing" value="Single Spaced" />
<label for="single_spaced">Single Spaced</label>
</div>

<div>
<h5>No. of pages</h5>
<input class="changedVal" type="number"  name="no_pages" min="1" max="10" value="1" />
</div>

<div class="switch-field" style="margin-bottom:25px;">
<h5>Deadline</h5>
<input class="changedVal" type="radio" id="14_days" name="deadline" value="14 Days" />
<label for="14_days">14 Days</label>
<input class="changedVal" type="radio" id="7_days" name="deadline" value="7 Days" />
<label for="7_days">7 Days</label>
<input class="changedVal" type="radio" id="5_days" name="deadline" value="5 Days" />
<label for="5_days">5 Days</label>
<input class="changedVal" type="radio" id="3_days" name="deadline" value="3 Days" />
<label for="3_days">3 Days</label>
<input class="changedVal" type="radio" id="48_hrs" name="deadline" value="48 Hrs" />
<label for="48_hrs">48 Hrs</label>
<input class="changedVal" type="radio" id="24_hrs" name="deadline" value="24 Hrs" />
<label for="24_hrs">24 Hrs</label>
</div>

<input style="margin-left:-300px;margin-top:0px;width:200px;border:none;background-color:transparent;font-size:30px;color:#32c5d2;" type="text" id="testVal">

<script type="text/javascript" >
var coun=0;

$('.changedVal').change(function(){

var level=$('input[name=academic_level]:checked').val();
var spacing=$('input[name=spacing]:checked').val();
var pages=$('input[name=no_pages]').val();
var deadline=$('input[name=deadline]:checked').val();

var data={academic_level:level,spacing:spacing,pages:pages,deadline:deadline};

console.log(data);
$.ajax({
url: '/pricecalculator',
type: 'post',
data: data,
success: function(msg){

console.log(JSON.stringify(msg));

 $('#testVal').val(JSON.stringify(msg));
 }
 });
 });
 </script>

 </form>

Below is my contoller code

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Http\Requests;
    use Illuminate\Support\Facades\Input;
    use App\Http\Controllers\Controller;
    use App\Prices;
    use View;

    class MainController extends Controller
    {
    public function priceCalculator(Request $request)
    {
    $level = Input::get("academic_level");
    $spacing = Input::get("spacing");
    $pages = Input::get("pages");
    $deadline = Input::get("deadline");

    $prices = Prices::where('academic_level',$level)
                    ->where('spacing',$spacing)
                    ->where('deadline',$deadline)
                    ->pluck('price');

    return  $prices;
    }
    }

Below is my route

    Route::post('/pricecalculator', array('uses' => 'MainController@priceCalculator'));

The output that i get is ["100"] but my desired output is 100....how do i go about achieving this as the returned value

  • 写回答

1条回答 默认 最新

  • dqfaom241360 2017-08-17 08:37
    关注

    In Laravel you can pick the first value of a collection using using the first method:

    $price = Prices::where('academic_level',$level)
                    ->where('spacing',$spacing)
                    ->where('deadline',$deadline)
                    ->pluck('price')
                    ->first();
    

    pluck() returns an array, as there might be multiple rows matching your criteria.

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

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch