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

数组转换为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.

    打赏 评论

相关推荐 更多相似问题