【问题标题】:insert data in multiple table with one function in laravel在laravel中使用一个函数在多个表中插入数据
【发布时间】:2017-03-23 20:56:30
【问题描述】:

我正在尝试在具有相同功能的多个表中添加值,但我收到一个错误,即 id 和 product_id 不能为空!!即使它们已设置。这是我的代码:

$parentproduct=new Product();
$parentproduct->id=Input::get('id');
$insertedId = $parentproduct->id;
$parentproduct->save();

$product=new ProductsTranslation();


$product->id=Input::get('id');
$product->product_id =Input::get('insertedId');
$product->title=Input::get('title');
$product->content=Input::get('content');
$product->price=Input::get('price');
$product->description_title=Input::get('description_title');
$product->prod_info_title=Input::get('prod_info_title');
$product->prod_info=Input::get('prod_info');
$product->save();

【问题讨论】:

    标签: mysql laravel


    【解决方案1】:

    看起来你需要在这里移动一些东西......

    在您运行 `->save() 之前,此 $insertedId = $parentproduct->id; 不会返回值。

    另外,您的第二条语句试图获取Input::('insertedId'),但您在上面设置了一个变量。

    试试这个:

    $parentproduct = new Product();
    $parentproduct->id = Input::get('id');
    $parentproduct->save();
    $insertedId = $parentproduct->id;
    
    $product = new ProductsTranslation();   
    $product->id = Input::get('id');
    $product->product_id = $insertedId;
    $product->title = Input::get('title');
    $product->content = Input::get('content');
    $product->price = Input::get('price');
    $product->description_title = Input::get('description_title');
    $product->prod_info_title = Input::get('prod_info_title');
    $product->prod_info = Input::get('prod_info');
    $product->save();
    

    【讨论】:

    • 谢谢,在最后使用这条线可以正常工作 $parentproduct->translation()->save($product);你能帮我添加 foreach 以在第二个表中添加多行语言变量吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    相关资源
    最近更新 更多