【问题标题】:I want to insert update the content of table when inserting from another table从另一个表插入时,我想插入更新表的内容
【发布时间】:2020-04-13 14:03:38
【问题描述】:

我有一个 Laravel 代码,用于将数据插入表中,但我想根据插入项目的详细信息更新另一个表的内容;下面是我的控制器代码

public function docadd(Request $request){


        $doc=new documents();
         $doc->claim_id=request('id');
         $doc->file_name=request('new');
         $doc->cat=request('new');
         $doc->type='needed';
         $doc->description='needed';
        $doc->save();

            $id=$doc->claim_id;
            $nm=$doc->file_name;
            $dc=options::where('claim_id', $id)
                    ->get();
                    $ed=$dc[0][$nm];
            dd($dc[0]->$ed);  //this dd returns "Doc2"
            $dc->$ed=''; i want it to update the content of the table column with the column with 
  name of the dd value
            $dc->save();
        return redirect()->back();
    }

请问我真的需要这方面的帮助

【问题讨论】:

  • 现在有什么问题?你能再解释一下吗?
  • 问题是它没有更新第二张表
  • 我希望它转到表选项并获取具有 $ed=$dc[0][$nm]; 返回的名称的名称的列并将其值设置为空
  • 如果您知道如何使用 Eloquent 模型,请查看这里:stackoverflow.com/questions/36999557/…
  • 方法 Illuminate\Database\Eloquent\Collection::update 不存在。这是我尝试更新时遇到的错误

标签: php laravel


【解决方案1】:

我认为您本身就有逻辑错误。如果要更新数据,使用first()而不是get(),它将返回初始值。可能是这样的:

 public function docadd(Request $request){
     $doc= new documents();
     $doc->claim_id=request('id');
     $doc->file_name=request('new');
     $doc->cat=request('new');
     $doc->type='needed';
     $doc->description='needed';
    $doc->save();
        //$id=$doc->claim_id;
        $nm=$doc->file_name;
        $dc=options::where('claim_id', $doc->claim_id)
         ->first();
        //Also you can use like this 
        //$dc= options::findOrFail($doc->claim_id);
        $dc->$ed=''
        $dc->save();
    return redirect()->back();
}

如果我理解你的话,你可以做点什么。

【讨论】:

  • $ed 未声明,我之前将我的声明为 $ed=$dc[0][$nm];
  • 我认为你不能这样做:$dc->$ed='',这样做 `$dc->ed="someting or variable also"``。我刚刚给你指路了。
  • 函数 Illuminate\Database\Eloquent\Model::setAttribute() 的参数太少,在 C:\Users\G_BEN\Desktop\project\vendor\laravel\framework\src\Illuminate\ 中传递了 1 个Database\Eloquent\Concerns\HasAttributes.php 在第 615 行,而我在运行代码时得到的正是 2 个预期的结果
  • 公共函数 docadd(Request $request){ $doc= new documents(); $doc->claim_id=request('id'); $doc->file_name=request('new'); $doc->cat=request('new'); $doc->type='需要'; $doc->description='需要'; $doc->保存(); //$id=$doc->claim_id; $nm=$doc->文件名; $dc=options::where('claim_id', $doc->claim_id) ->first(); //你也可以像这样使用 //$dc= options::findOrFail($doc->claim_id); $ed=$dc[0][$nm]; $dc->$ed=''; $dc->保存();返回重定向()->返回(); }
猜你喜欢
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
相关资源
最近更新 更多