2016-08-19 3 views
0

이 코드가 있습니다.Many to Many Ajax Laravel 5.2

public function items($subcategory_id){ 

    $items = $this->ajax->items($subcategory_id); 

    return Response::json([ 
      'success' => 'true', 
      'items' => $items 
     ]); 
} 

그리고 출력은 다음과 같습니다 enter image description here

당신이 많은 많은에 unit_id 참조를 볼 수 있습니다 당신은 자바 스크립트의 unit_id의 이름을 얻을 수를 조작 할 수있다. 루프를 만들어 자신 만의 배열을 만들거나 할 수있는 함수가 필요합니까? 내 저장소 코드는 다음과 같습니다.

public function items($subcategory_id){ 

    $this->modelName = new Subcategory(); 
    return $this->modelName->find($subcategory_id)->items; 
} 

내 모델

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Subcategory extends Model 
{ 
    protected $table = 'subcategories'; 
    protected $fillable = [ 
     'qty', 'desc', 'unit_price', 'ext_price' 
    ]; 
    public function items(){ 

     return $this->belongsToMany('App\Item', 'item_subcategory', 'subcategory_id', 'item_id')->withTimestamps(); 
    } 
} 
+0

당신이해야 할 것은 열망로드를 사용합니다. 그러나 나는 당신의 모델과 관계가 무엇인지 이해하지 못합니다. 모델 코드 게시. – PawelMysior

+0

public function items() { \t return $ this-> belongsToMany ('App \ Item', 'item_subcategory', 'subcategory_id', 'item_id') -> withTimestamps(); } – Rbex

+0

json 구조를 자바 스크립트에 보내려면 어떻게해야합니까? – KmasterYC

답변

1

사용 열망로드. 이 관련으로 특정 Subcategory를 가져옵니다 Items 및 해당 항목에 관한 Units :

Subcategory::where('id', $subcategoryId)->with('items.unit')->get(); 
+0

감사합니다 그것은 매력처럼 작동합니다. – Rbex