无法添加或更新子行' user_id'

时间:2016-03-06 14:45:10

标签: php mysql laravel

当我提交表单时,我可以在错误中看到我的所有值都被传递,除了' user_id':

('insert into `products` (`title`, `body`, `address`, `condition`, `quantity`, `price`, `bid_price`, `img_1`, `updated_at`, `created_at`)

但是缺少'user_id',为什么?这是我的Product.php

protected $fillable = ['slug', 'title', 'body', 'user_id', 'price', 'bid_price', 'address', 'condition', 'quantity', 'img_1', 'img_2', 'img_3', 'img_4', 'views'];

控制器中的功能:

public function storeAuction(AuctionSubmitRequest $request)
{   
    $product = Product::create($request->all());

    Auth::user()->product()->save($product);

    $request->file('img_1')->move(public_path('images'), $request->file('img_1')->getClientOriginalName());

    $data = $request->except(['img_1']);
    $data['img_1'] = public_path('images') . '/' . $request->file('img_1')->getClientOriginalName();

    $product->create($data);

    return redirect('/');

}

我的表格:

<form id="tryitForm" class="form-horizontal" method="POST" action="/paskelbti-nauja-aukciona" enctype="multipart/form-data">
    {!! csrf_field() !!}
    <div class="form-group">
        <label class="col-md-3 control-label">Prekės pavadinimas</label>
        <div class="col-md-4">
            <input type="text" class="form-control" name="title" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Prekės aprašymas</label>
         <div class="col-md-4">
            <textarea name="body" class="form-control"></textarea>
        </div>      
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Adresas</label>
        <div class="col-md-6">
            <input type="text" class="form-control" name="address" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Pristatymas</label>
        <div class="col-md-6">
            <div class="radio">
                <label><input type="radio" name="dispatch" value="kurjeris" /> Kurjeriu</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="dispatch" value="pastas" /> Paštu</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="dispatch" value="kita" /> Kita (parašysiu "prekės aprašyme")</label>
            </div>
        </div>
    </div>


    <div class="form-group">
        <label class="col-md-3 control-label">Prekės būklė</label>
        <div class="col-md-6">
            <div class="radio">
                <label><input type="radio" name="condition" /> Nauja</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="condition" /> Labai gera</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="condition" /> Gera</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="condition" /> Panaudota</label>
            </div>    
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Barkodas (jei turite)</label>
        <div class="col-md-4">
            <input class="form-control" name="barcode" type="number" min="0" />
        </div>      
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Prekių kiekis</label>
        <div class="col-md-2">
            <input class="form-control" name="quantity" type="number" min="1" />
        </div>      
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Prekės nuotraukos</label>
        <div class="col-md-6">
            <input type="file" class="form-control" name="img_1" /><br>
            <input type="file" class="form-control" name="img_2" /><br>
            <input type="file" class="form-control" name="img_3" /><br>
            <input type="file" class="form-control" name="img_4" />
        </div>      
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Prekės kaina</label>
        <div class="col-md-2">
            <input class="form-control" name="price" type="number" step="0.01" min="0.01" />
        </div>      
    </div>


    <div class="form-group">
        <label class="col-md-3 control-label">Minimali siūlymo suma</label>
        <div class="col-md-2">
            <input class="form-control" name="bid_price" type="number" step="0.01" min="0.01" />
        </div>      
    </div>

    <div class="form-group">
        <div class="col-md-offset-3 col-md-8">
        <button type="submit" class="btn btn-primary">Pradėti aukcioną</button>
    </div>
</div>
</form>

1 个答案:

答案 0 :(得分:0)

您可以尝试以下内容。

public function storeAuction(AuctionSubmitRequest $request)
{
    $request->file('img_1')->move(public_path('images'), $request->file('img_1')->getClientOriginalName());

    // first create the related model, do not save or create 
    // because you will get a 'Cannot add or update a child row'
    $product = new Product();
    $product->property1 = $request->get('property1');
    $product->property2 = $request->get('property2');
    // ...
    $product->img_1 = public_path('images') . '/' . $request->file('img_1')->getClientOriginalName();

    // here you save a product associated to the user.
    Auth::user()->product()->save($product);

    return redirect('/');

}