2017-04-23 12 views
2

나는 다음 한 코드를 반환 테이블 vehicles가 구성쿼리 객체 (빌더), 정의되지 않은 속성

public function detailCustomer(Customer $customer) 
    { 
     $vehicles = DB::table('vehicles') 
        ->selectRaw('*') 
        ->where('cust_id', '=', $customer->id); 
     return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles)); 
    } 

다음 customers.detail보기에서

spz 
cust_id <FK> //this is foreign key to customer->id 
type 
brand 

, 나는 데이터를 표시하기 위해 다음 코드를 사용하려고 하지만이 오류가 발생합니다 :

Undefined property: Illuminate\Database\PostgresConnection::$spz ,코드 : 나는 this topic을 읽을 수는 있지만 내가 객체를 통해 반복하는 foreach를 사용하기 때문에 내 문제가 아니에요 보인다하지만 오류로 인해 페이지에 내 $vehicles 변수로 데이터베이스에서 개체를하지 않는 것 같습니다있다

@if (count($vehicles) > 0) 
    <?php $i = 1; ?> 

    @foreach ($vehicles as $vehicle) 
    <?php $i++; ?> 
    <td>{{$vehicle->spz}}</td> 
    <td>{{$vehicle->type}}</td> 
    <td>{{$vehicle->brand}}</td> 
    @endforeach 
@endif 

, 또한 이런 식으로 뭔가 보여줍니다

'customer' => object(Customer), 'vehicles' => object(Builder) 

customer가 올바르게 개체를 얻을 수 있음을 어떻게 생각하게,하지만 vehiclesBuilder을 얻는다 ?? 거기에 무엇이 잘못되었는지 전혀 모르겠습니다. 어떤 아이디어? 제가 작업중인 프로젝트에서 내가하고있는 일을 설명하기 위해 버튼을 클릭하여 세부 정보 페이지 (프로필 페이지)에 차량을 추가하는 고객 세부 정보 페이지가 있고 고객에게 id을 매개 변수로 보내면 나는 데이터베이스에 차량을 추가하는데, 작동한다 (차량은 고객이 정확히 추가되었다. id). 이제는 위의 코드와 같은 차량 정보가있는 세부 정보 페이지를 표시하려고 할 때 문제가 나타납니다. 충분히 명확하길 바랍니다. 제안에 감사드립니다.

+4

로 교체하려고합니다 -> 끝에') (취득 ? – Joe

답변

1

시도는 컨트롤러

public function detailCustomer(Customer $customer) 
{ 
    $vehicles = DB::table('vehicles') 
       ->selectRaw('*') 
       ->where('cust_id', '=', $customer->id)->get(); 
       // ->where('cust_id', '=', $customer->id)->paginate($YOUR_LIMIT_ONE_PAGE); 
    return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles)); 
} 

->get() 또는 ->paginate($YOUR_LIMIT_ONE_PAGE)를 추가하고 쿼리가`필요하지 않습니다 foreachforelse

@forelse ($vehicles as $index => $vehicle) 
    <tr> 
     <td>{{$index}}</td> 
     <td>{{($vehicle->spz !== null) ? $vehicle->spz : '-'}}</td> 
     <td>{{($vehicle->type !== null) ? $vehicle->type : '-'}}</td> 
     <td>{{($vehicle->brand !== null) ? $vehicle->brand : '-'}}</td> 
    </tr> 
@empty 
    <td colspan='4'>Data not found</td> 
@endforelse 
+0

고마워요, 작동합니다! Btw, 나는 'forelse'를 대신하지 않았지만 차량이없는 경우를 대비하여 그 가정을 가정합니다. 맞습니까? 왜 '페이지 매김'을 추가하면 효과가 있습니까? 나는 그것을 얻지 않는다. – Ady96

+0

@ Ady96 예. 'forelse'는 당신의 질의가 비어있는 필터 일 수 있습니다. –

+0

@ Ady96 당신은'get()'을 사용하면 모든 결과를 얻을 수 있습니다. '페이징 (paginate) '하면 결과를 페이징 할 수 있습니다. 그것은 귀하의 사이트가 무거운로드하지 않습니다. –