【问题标题】:Select whether the item is on sale (yes/no)选择商品是否在售(是/否)
【发布时间】:2022-01-24 08:05:50
【问题描述】:

我创建了一个基本的 Laravel 库存应用程序,并允许用户在验证后将数据输入到浏览器中。我的视图页面上有一个选择元素,用户需要在该元素中说明该商品是否在销售中是/否。当我输入一个项目并且它确实说是并将它添加到浏览器上的库存列表时,我可以让它工作。但是当我选择否时,它会显示“特价字段必须标记为是或否”并且不会显示在库存清单上。关于如何解决这个问题的任何想法?

这是我的 create.blade.php 文件:

@extends('layouts.app')

@section('title', 'Create Inventory')

@section('content')
    <h1><strong>Please fill in the blanks to create an inventory.</strong></h1>
    <p><a href="/">Click here</a> to go back to the homepage.</p>
    <p><a href="/inventories">Click here</a> to go to the generated inventory page.</p>
    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    <form action="/inventories" method="post">
        @csrf
        <label for="title">Enter an item name:</label>
        <input type="text" name="title" required /><br><br>

        <label for="description">Enter the item's description:</label>
        <textarea name="description" required></textarea><br><br>

        <label for="price">Enter the item's price:</label>
        <input type="number" name="price" required/><br><br>

        <label for="in_stock">Enter a number of items in stock:</label>
        <input type="number" name="in_stock" required/><br><br>

        <label for="on_sale">Select yes/no if item is on sale:</label>
        <select name="on_sale">
            <option value="">--Please choose an option--</option>
            <option value="1">Yes</option>
            <option value="2">No</option>
        </select><br><br>

        <button type="submit">Submit</button>
    </form>
@endsection

这是我的 InventoryController.php:

<?php

namespace App\Http\Controllers;

use App\Models\Inventory;
use Illuminate\Http\Request;

/**
 * Class InventoryController
 */
class InventoryController extends Controller
{
    /**
     * Get the list of inventories.
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function index()
    {
        $inventories = Inventory::all();
        return view('pages.inventories',[
            "inventories" => $inventories
        ]);
    }
    /**
     * Show the generated inventory
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function create()
    {
        return view('pages.inventories.create');
    }
    /**
     * Store a newly created inventory
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title'=> 'required|string',
            'description'=> 'required|string|max:300',
            'price' => 'required|integer|min:0',
            'in_stock' => 'required|integer',
            'on_sale' => 'required|boolean'
        ]);
        $inventory = new Inventory();

        $inventory->fill($validated)->save();

        return redirect('/inventories');
    }
}

这是我的inventories.blade.php:

@extends('layouts.app')

@section('title', 'My Inventory')

@section('content')
    <h1>Inventory Table</h1>
    <p>This is the inventory table made using PHP Laravel.</p>
    <p><a href="/inventories/create">Click here</a> to create your own inventory table.</p>
    <p><a href="/">Click here</a> to go back to the homepage.</p>
   <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Description</th>
                <th>Price</th>
                <th>In stock</th>
                <th>On sale</th>
            </tr>
        </thead>
        <tbody>
            @foreach($inventories as $inventory)
            <tr>
                <td>{{$inventory->id}}</td>
                <td>{{$inventory->title}}</td>
                <td>{{$inventory->description}}</td>
                <td> &pound;{{ number_format($inventory->price, 2) }}</td>
                <td>{{$inventory->in_stock}}</td>
                <td>{{ $inventory->on_sale ? 'Yes' : 'No' }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
@endsection

这是我的库存模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Inventory extends Model
{
    use HasFactory;

    /**
     * @var array
     */
    protected $casts = [
        'on_sale' => 'boolean',
    ];
    /**
     * @var string[]
     */
    protected $fillable = [
        'title',
        'description',
        'price',
        'in_stock',
        'on_sale',
        'updated_at',
        'created_at'
    ];
}

【问题讨论】:

    标签: php laravel boolean


    【解决方案1】:

    这是由于 value 您已分配了您的 No 选项以及您期望并验证 valueboolean 的事实:

    'on_sale' => 'required|boolean'
    

    如果您将No 选项的value 更改为0,您的验证将通过。

    【讨论】:

    • 我试过没有 2、0 和空字符串。当我尝试输入数据说 on sale 字段需要为 true 或 false 时,仍然会弹出相同的窗口。
    • @AlexV 是什么引发了错误,您的验证还是数据库? You can see a working example of the above here.
    • 验证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多