【问题标题】:Set product as featured in laravel将产品设置为 laravel 中的特色
【发布时间】:2016-12-20 07:57:57
【问题描述】:

当时我对如何仅将一种产品标记为特色产品有点困惑。我在产品表featured 中添加了列,其中接受0 用于普通产品,1 用于特色产品。

1 的特色产品只能是一种产品

所以我在显示所有产品的下拉菜单中放入了刀片

{{ Form::open() }}
<div class="form-group">
    <label for="title" class="control-block">Assign Product as Featured:</label>
        <select class="form-control" name="featured">       
            @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() }}

<p>Current Featured Product: <strong>@if($featured->featured == 1){{ $featured->title }}@endif</strong></p> 

因此,我在下拉列表中显示所有产品,管理员可以从中选择另一个产品并将其标记为特色。已经在下拉列表中销售当前产品。

这是我放入控制器的内容

public function products() {
    $products = Product::all();
    return View::make('site.admin.products', [
        'products' => $products
    ]);
}

public function featuredProduct($productId) {

    $product = Product::where('product_id', $productId)->first();
    if (!$product) {
        App::abort(404);
    }

    $product_featured = Input::get('featured', $product->featured);
    $product->featured = $product_featured;
    $product->save();
    return Redirect::to('/admin/products');
}

还有路线

Route::get ('/admin/products', ['uses' => 'AdminController@products', 'before' => 'admin']);
Route::post ('/admin/products/{productId}', ['uses' => 'AdminController@featuredProduct', 'before' => 'admin']);

我如何在控制器中创建逻辑,以便将我在下拉列表中选择的产品更新为1,并将当前产品更新为数据库中的0

目前的错误是

production.ERROR: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    您的表单应打开为:

    {{ Form::open(['url' => '/admin/products/feature', 'method' => 'post']) }}
    

    你的路线应该是:

    Route::post('/admin/products/feature', ['uses' => 'AdminController@featuredProduct', 'before' => 'admin']);
    

    你可以把你的逻辑写成:

    public function featuredProduct() {
        $product_featured_id = Input::get('featured');
    
        $product = Product::where('product_id', $product_featured_id)->firstOrFail();
    
        Product::where('featured', 1)->update(['featured' => 0]); // this will make all product of featured 0
    
        $product->featured = 1;
        $product->save();
        return Redirect::to('/admin/products');
    }
    

    【讨论】:

    • 谢谢,但我在此行收到错误 ErrorException: Undefined variable: productId in$product = Product::where('product_id', $productId)-&gt;firstOrFail();
    • 这样可以正确更新。只是你知道为什么在更改产品后这条线不再显示当前产品&lt;p&gt;Current Featured Product: &lt;strong&gt;@if($featured-&gt;featured == 1){{ $featured-&gt;title }}@endif&lt;/strong&gt;&lt;/p&gt;
    • 试试这个&lt;p&gt;Current Featured Product: &lt;strong&gt;{{ $products-&gt;where('featured', 1)-&gt;first() ? $products-&gt;where('featured', 1)-&gt;first()-&gt;title : '' }}&lt;/strong&gt;&lt;/p&gt;
    • 收到production.ERROR: Symfony\Component\Debug\Exception\FatalErrorException: Uncaught TypeError: Argument 1 passed to Illuminate\Exception\PlainDisplayer::display() must be an instance of Exception, instance of Error given
    【解决方案2】:

    你需要设置表单动作

    {{ Form::open(array('url' => '/admin/products/' . $product_id)) }}
    

    但我觉得这里缺少一些东西

    【讨论】:

    • 谢谢。是的,它缺少控件中的逻辑,它应该从 0/1 切换功能,并且只有一个产品与 1 一起使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2021-08-05
    • 2021-05-08
    • 2018-01-30
    • 2019-08-22
    相关资源
    最近更新 更多