Php Laravel未将Json数据插入MySQL

Php Laravel未将Json数据插入MySQL,php,laravel,Php,Laravel,请参阅下面的代码,无法找出错误,但插入未发生。。。有没有关于如何调试/修复的想法 Laravel-控制器 Laravel-路线 Route::post('create_phone_contacts/{id}', 'CreatePhoneContactsController@create'); foreach循环有点问题-它似乎可以很好地处理循环外部的硬编码值删除dd()并在for循环中创建对象。这会解决你的问题 namespace App\Http\Controllers; use App\

请参阅下面的代码,无法找出错误,但插入未发生。。。有没有关于如何调试/修复的想法

Laravel-控制器

Laravel-路线

Route::post('create_phone_contacts/{id}', 'CreatePhoneContactsController@create');
foreach循环有点问题-它似乎可以很好地处理循环外部的硬编码值

删除dd()并在for循环中创建对象。这会解决你的问题

namespace App\Http\Controllers;
use App\Models\PhoneContactsPhonesModel;
use Illuminate\Http\Request;

class CreatePhoneContactsController extends Controller
{
    public function create(Request $request, $id)
    {  
        $json = json_decode($request->getContent(), true);
        foreach ($json as $key => $value) {
            $users = new PhoneContactsPhonesModel;// ---> here
            $users->mysql_user_id = $id;
            $users->phone = $key;
            $users->name = $value;
            $users->save();
        }
    }
}
删除dd()并在for循环中创建对象。这会解决你的问题

namespace App\Http\Controllers;
use App\Models\PhoneContactsPhonesModel;
use Illuminate\Http\Request;

class CreatePhoneContactsController extends Controller
{
    public function create(Request $request, $id)
    {  
        $json = json_decode($request->getContent(), true);
        foreach ($json as $key => $value) {
            $users = new PhoneContactsPhonesModel;// ---> here
            $users->mysql_user_id = $id;
            $users->phone = $key;
            $users->name = $value;
            $users->save();
        }
    }
}
您只需从代码中删除dd()

更改此设置

$json = dd(json_decode($request->getContent(), true));  //change here
到此

$json = json_decode($request->getContent(), true); //change this remove dd()
它会起作用的。

您只需从代码中删除dd()

更改此设置

$json = dd(json_decode($request->getContent(), true));  //change here
到此

$json = json_decode($request->getContent(), true); //change this remove dd()

它会起作用的。

显示了什么错误?在邮递员中没有错误。。只需返回您正在使用的阵列
dd()
。该函数将转储该值并终止脚本的执行,因此foreach循环将永远不会执行..好的,删除dd只会插入1行-这是最后一行,这是因为您在每次迭代中反复更新和保存同一对象。您正在尝试更新现有用户还是创建新用户?显示了什么错误?在postman中没有错误。。只需返回您正在使用的阵列
dd()
。该函数将转储该值并终止脚本的执行,因此foreach循环将永远不会执行..好的,删除dd只会插入1行-这是最后一行,这是因为您在每次迭代中反复更新和保存同一对象。您正在尝试更新现有用户还是创建新用户?