2016-04-02 31 views
0

, vue-resource adlı bir eklenti ile vuejs ajax çağrısı kullanarak bir veritabanından veri almaya çalışıyor. Maalesef, json veri nesnesi, veritabanından gerçek verileri değil, html sayfasını içerir. Birisi bana neyi yanlış yaptığımı söyleyebilir mi?Vuejs ajax GET isteği, verileri Laravel 5.1 Blade şablonunda döndürmüyor.

<?php 
Route::get('find', '[email protected]'); 

fruitsctrl.php (kontrol)

routes.php

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Fruit; 
use App\Http\Requests; 
use App\Http\Controllers\Controller; 

class FruitsCtrl extends Controller 
{ 
    public function index(Request $req){ 
     $fruits = Fruit::all(); 
     return view('fruitsHome', $fruits); 
    } 
} 

fruit.php (model)

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Fruit extends Model 
{ 
    protected $fillable = [ 
      'id', 'name', 'type' 
    ]; 
} 

fruitshome.blade: Benim kodudur. php (görünüm)

Ayrıca böyle indeks yöntemi güncellemeniz gerekir

class Fruit extends Model 
{ 

    protected $table = 'fruits'; 
    protected $fillable = [ 
     'id', 'name', 'type' 
    ]; 
} 

:

<head> 
     <meta id="token" content="{{ csrf_token() }}"> 
</head> 
<body> 
    <div class="row" id="vapp"> 
     <ul> 
      <li v-for="fruit in fruits"> 
       @{{ fruit.name }} 
       @{{ fruit.type }} 

      </li> 
     </ul> 
    </div> 
<body> 

app.js (javascript)

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content'); 
var v = new Vue({ 
    el : "#vapp", 
    ready :function() { 
     this.fetchFruits(); 
    }, 
    data : { 
     fruit : {id:'', name:'', type:''}, 
     fruits : [] 
    }, 
    methods : { 
     fetchFruits : function(){ 
      this.$http.get('/find').then(function(res){ 
       this.fruits = res; 
      },function (data){ 
       console.log(error ...); 
      }); 
     } 
    } 
}); 

cevap

0

Ben böyle modelinde tablo adını ayarlamak gerektiğini düşünüyorum

public function index(Request $req){ 
    $fruits = Fruit::all(); 
    return view('fruitsHome')->withFruits($fruits); 
} 

ve son olarak bıçak güncellemek:

 <li v-for="fruits in fruit"> 
      @{!! $fruits->name !!} 
      @{!! $fruits->type !!} 

     </li> 

size Şu anda denetleyici bir görünüm dönen

+0

Cevabınız için teşekkür ederiz. Önerinizi denedim ama işe yaramıyor. \ View \ View.php'yi aydınlatırken bir hata istisnası alıyorum. Hata yasadışı ofset tipidir ... – DBoonz

1

yardımcı olur bana bildirin: doğrudan, Etkili sonuçlar döndürebilir yerine

class FruitsCtrl extends Controller 
{ 
    public function index(Request $req){ 
     $fruits = Fruit::all(); 
     return view('fruitsHome', $fruits); 
    } 
} 

ve onlar çıktı olacağım JSON:

class FruitsCtrl extends Controller 
{ 
    public function index(Request $req){ 
     $fruits = Fruit::all(); 
     return $fruits; 
    } 
}