1
내가 Laravel 4.2에 등장하는 /하지 기능을 갖춘 제품에 대한 기능을 만들려고 노력하고있어, 함께 등장하지하지만 난 정의되지 않은 메서드를 분명히 \ 데이터베이스 \ 쿼리 \ 빌더 오류메이크업 제품은 기능/Laravel
전화 받기 :: 이것은 내 컨트롤러 기능
public function featuredProduct() { $product_featured_id = Input::get('featured'); $product = Product::where('product_id', $product_featured_id); Product::where('featured', 1)->update(['featured' => 0]); // make all other products featured -> 0 $product->featured = 1; $product->save(); return Redirect::to('/admin/products')->with('message', 'Product marked as featured!'); }
입니다) (
을 저장하고이 뷰
0123에서 양식입니다{{ Form::open(['url' => '/admin/products/feature', 'method' => 'post']) }}
<div class="form-group">
<select class="form-control" name="featured">
<option value="0">Remove from Featured</option>
@foreach($products as $featured)
<option value="{{ $featured->product_id }}" {{ $featured->featured == 1 ? "selected" : ""}}>{{ $featured->title }}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Make Product Featured</button>
{{ Form::close() }}
제품이있는 표에는 기능이있는 제품의 경우 , 기능이없는 제품의 경우 0
을 저장하는 열이 있습니다.
내가 여기에서 무엇을 놓치고 있습니까?
갱신 : 당신이 개체 및 쿼리 빌더 아닌 인스턴스를 얻을 것이다,이 경우
$product = Product::where('product_id', $product_featured_id)->first();
:
public function featuredProduct() {
$product_featured_id = Input::get('featured');
if($product_featured_id == 0)
{
Product::where('featured', 1)->update(['featured' => 0]); // make all other products featured -> 0
}
else
{
$product = Product::where('product_id', $product_featured_id)->first();
Product::where('featured', 1)->update(['featured' => 0]); // make all other products featured -> 0
$product->featured = 1;
$product->save();
}
return Redirect::to('/admin/products')->with('message', 'Product marked as featured!');
}
답변 해 주셔서 감사합니다. 내가 추천 할 제품을 선택하면이 기능이 작동합니다. 하지만 'Remove from Featured'옵션을 선택하면 'ErrorException : 빈 값으로부터 기본 객체 생성하기'오류가 발생합니다. – VLS
작성한 방법으로 내 질문을 업데이트했습니다. 일하는 것 같지만 좋은 접근입니까? 최적화 할 수 있습니까? – VLS