Laravel 5 - 将数据更新为2个相关表

时间:2015-08-19 08:38:50

标签: php laravel laravel-5

我有2个表彼此相关:

Query Builder

我想使用Laravel更新我的表格。我在互联网上做了一些研究,但他们只介绍了如何将信息更新到只有1个表。我尝试使用DB::table('goals') ->where('goals.goalId', '=', $goaldata['goalid']) ->where('goals.goalId', '=', 'frequencies.goalId') ->update([ // my query ]); 但是,它不起作用。这是我的疑问:

$fullname=$_POST['fullname'];
$N = count($fullname);
for($i = 0 ;$i< count($fullname); $i++) {
$query = "UPDATE famcomp SET app_id = '".$_POST["app_id[$i]"]."', fullname = '".$_POST["fullname[$i]"]."', fage = '".$_POST["fage[$i]"]."', frel = '".$_POST["frel[$i]"]."', fcivil = '".$_POST["fcivil[$i]"]."', fedu = '".$_POST["fedu[$i]"]."', foccup = '".$_POST["foccup[$i]"]."', finco = '".$_POST["finco[$i]"]."' WHERE app_id = '".$_POST["app_id[$i]"]."'"; // Run the Mysql update query inside for loop
mysql_query($query, $con);
$message = 'Success Updating the record!!';
echo "<SCRIPT>alert('$message');</SCRIPT>";
echo "<script>windows: location='editrecord.php?name=$a'</script>";
}

如果您想查看我的查询以获取更多详细信息,这是我的完整陈述:http://pastebin.com/CmbReqGq
如何将数据更新为2个表? 谢谢。

2 个答案:

答案 0 :(得分:0)

如果您正在使用迁移,则可以使用onUpdate('cascade'),它将在更新父表时更新单个字段。

或者,如果您想要更新大量字段,可以使用mysql触发器,当触发事件时会触发该触发器。像这样:

DB::unprepared("
            CREATE TRIGGER new_your_trigger AFTER UPDATE ON goals FOR EACH ROW
            BEGIN

             UPDATE frequencies SET id = NEW.id ;

            END
        ");

答案 1 :(得分:0)

这简单得多

$data = ['id' => 1,'name' =>'John'];
$query = Model::update($data);
// to get the update row id
return $query -> id;

使用return id插入或更新相关表中的数据

相关问题