Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Simone94's avatar

Save Json data to Database Laravel

Hi, i'm new in this forum. I would need to save some data in json format in a database with laravel. The data is sent from a raspberry via python's requests.post function to my laravel (the data is from a scan to detect MAC address and RSSI signal of Bluetooth devices). If I go into the DB I notice that in the column of the table where I would like the data to be saved, the column is empty. What am I doing wrong? For this project I'm using Pycharm to develop the code in Python and Visual Studio for the Laravel part. Below is the python code that creates the Json and sends it to Laravel

def readFile():

with open("/home/pi/Desktop/Progetti SIoTD/Bluetooth/device.txt", "r") as file:
    for i in file:
        line, *lines = i.split()
        if line in mac_dict:
            mac_dict[line] += lines
        else:
            mac_dict[line] = lines
print(mac_dict)
print("\n")

json_obj = json.dumps(mac_dict, indent=4) #encode json
print(json_obj)

r = requests.post(ip, json=json_obj)
print(r.text)

Here is the Laravel code of the controller:

class DataFromRaspController extends Controller {

public function index()
{
    $data=DataFromRasp::all();
    return view('backend.auth.user.dictionary', compact("data"));
}

public function create()
{
    
}

public function store(Request $request)
{
    
}

public function show(DataFromRasp $dataFromRasp)
{
    //
}

public function edit(DataFromRasp $dataFromRasp)
{
    //
}

public function update(Request $request, DataFromRasp $dataFromRasp)
{
    //
}

public function destroy(DataFromRasp $dataFromRasp)
{
    //
}

public function getDict(Request $request)
{   
    //file_put_contents('dump.txt', var_dump($request->all()));
    $data = json_encode($request);
    DataFromRasp::create($data);

    return back()->with('success', 'Data successfully store in json format.');
}

}

Here is the Laravel code of the Model:

class DataFromRasp extends Model { use HasFactory;

public $timestamps = false;
protected $primaryKey = 'id';

protected $fillable = ['device'];

protected $casts = [
    'device' => 'array',
];

}

And then the code of my Route web.php:

Route::get('dict', [DataFromRaspController::class, 'index'])->name('dict');

And api.php:

Route::post('dictionary', [DataFromRaspController::class, 'getDict'])->name('dictionary');

This is my JSON:

{

"48:CE:B8:09:7D:AD": [
    "-91"
],

"70:61:91:66:6B:1E": [
    "-81",
    "-69",
    "-79",
    "-65",
    "-74"
],

"4B:6F:42:53:EB:82": [
    "-84",
    "-73",
    "-81"
],

"77:87:80:27:2B:31": [
    "-83",
    "-70",
    "-82",
    "-71",
    "-79",
    "-71",
    "-79",
    "-71",
    "-80",
    "-72"
],

"43:FD:25:20:5A:7B": [
    "-76",
    "-84",
    "-76",
    "-88",
    "-77",
    "-86"
],

"C1:04:08:03:3C:05": [
    "-73"
],

"7F:D2:44:60:82:BD": [
    "-88",
    "-79",
    "-88"
],

"FE:47:DA:73:90:29": [
    "-55",
    "-43",
    "-59",
    "-35",
    "-46",
    "-38",
    "-49"
],

"62:46:58:C8:A9:9A": [
    "-68",
    "-77",
    "-68"
],

"68:13:E4:00:12:30": [
    "-68",
    "-81"
],

"4E:0D:D6:41:77:FA": [
    "-68",
    "-77",
    "-68",
    "-76",
    "-68",
    "-79",
    "-71"
],

"71:B0:03:A6:17:A5": [
    "-82",
    "-73",
    "-81",
    "-73"
],

"19:F2:64:21:35:B5": [
    "-74",
    "-65",
    "-77",
    "-69",
    "-78",
    "-69"
],

"5C:D5:BA:32:06:AD": [
    "-86"
],

"11:91:3D:57:0E:F0": [
    "-90",
    "-82"
],

"44:08:E7:5C:56:B3": [
    "-89",
    "-80"
],

"58:99:6F:53:65:C8": [
    "-77",
    "-85"
],

"54:3C:61:D8:5B:DD": [
    "-88",
    "-79"
],

"59:02:8C:78:5D:E1": [
    "-85",
    "-73",
    "-82",
    "-69",
    "-84",
    "-75",
    "-83",
    "-71"
],

"64:F0:DC:95:6E:16": [
    "-86",
    "-78"
],

"71:E5:42:BA:19:F4": [
    "-91",
    "-77",
    "-86",
    "-73",
    "-84",
    "-74",
    "-84"
],

"E1:63:14:57:89:25": [
    "-48",
    "-57",
    "-38",
    "-47"
],

"57:7D:E9:6F:06:24": [
    "-85"
],

"56:B2:4B:5B:7E:2E": [
    "-88",
    "-80"
],

"64:3D:02:D4:ED:4E": [
    "-76",
    "-64",
    "-72"
],

"0C:93:8F:E5:C6:2A": [
    "-76",
    "-66"
],

"3F:47:42:77:BD:9A": [
    "-64",
    "-83"
],

"3C:46:5E:20:9A:FE": [
    "-79",
    "-57",
    "-74"
],

"67:58:EE:A8:9D:CC": [
    "-64",
    "-80"
],

"C4:A5:DF:24:05:7E": [
    "-65"
],

"79:B8:3B:90:31:12": [
    "-89",
    "-71",
    "-88",
    "-75"
],

"75:8E:FE:9B:69:DA": [
    "-93"
],

"49:BE:B0:74:7A:71": [
    "-66",
    "-82"
],

"55:33:20:52:E1:EC": [
    "-75"
],

"45:D3:80:F3:A4:59": [
    "-79",
    "-71"
],

"44:CA:8A:EF:31:45": [
    "-92",
    "-78"
],

"5F:0D:99:F8:EE:94": [
    "-83"
]

}

and after running the Python code the response I get with the last print(r.text) is:

array(1) {

[0]=> string(2978) "{

"48:CE:B8:09:7D:AD": [
    "-91"
],

"70:61:91:66:6B:1E": [
    "-81",
    "-69",
    "-79",
    "-65",
    "-74"
],

"4B:6F:42:53:EB:82": [
    "-84",
    "-73",
    "-81"
],

"77:87:80:27:2B:31": [
    "-83",
    "-70",
    "-82",
    "-71",
    "-79",
    "-71",
    "-79",
    "-71",
    "-80",
    "-72"
],

"43:FD:25:20:5A:7B": [
    "-76",
    "-84",
    "-76",
    "-88",
    "-77",
    "-86"
],

"C1:04:08:03:3C:05": [
    "-73"
],

"7F:D2:44:60:82:BD": [
    "-88",
    "-79",
    "-88"
],

"FE:47:DA:73:90:29": [
    "-55",
    "-43",
    "-59",
    "-35",
    "-46",
    "-38",
    "-49"
],

"62:46:58:C8:A9:9A": [
    "-68",
    "-77",
    "-68"
],

"68:13:E4:00:12:30": [
    "-68",
    "-81"
],

"4E:0D:D6:41:77:FA": [
    "-68",
    "-77",
    "-68",
    "-76",
    "-68",
    "-79",
    "-71"
],

"71:B0:03:A6:17:A5": [
    "-82",
    "-73",
    "-81",
    "-73"
],

"19:F2:64:21:35:B5": [
    "-74",
    "-65",
    "-77",
    "-69",
    "-78",
    "-69"
],

"5C:D5:BA:32:06:AD": [
    "-86"
],

"11:91:3D:57:0E:F0": [
    "-90",
    "-82"
],

"44:08:E7:5C:56:B3": [
    "-89",
    "-80"
],

"58:99:6F:53:65:C8": [
    "-77",
    "-85"
],

"54:3C:61:D8:5B:DD": [
    "-88",
    "-79"
],

"59:02:8C:78:5D:E1": [
    "-85",
    "-73",
    "-82",
    "-69",
    "-84",
    "-75",
    "-83",
    "-71"
],

"64:F0:DC:95:6E:16": [
    "-86",
    "-78"
],

"71:E5:42:BA:19:F4": [
    "-91",
    "-77",
    "-86",
    "-73",
    "-84",
    "-74",
    "-84"
],

"E1:63:14:57:89:25": [
    "-48",
    "-57",
    "-38",
    "-47"
],

"57:7D:E9:6F:06:24": [
    "-85"
],

"56:B2:4B:5B:7E:2E": [
    "-88",
    "-80"
],

"64:3D:02:D4:ED:4E": [
    "-76",
    "-64",
    "-72"
],

"0C:93:8F:E5:C6:2A": [
    "-76",
    "-66"
],

"3F:47:42:77:BD:9A": [
    "-64",
    "-83"
],

"3C:46:5E:20:9A:FE": [
    "-79",
    "-57",
    "-74"
],

"67:58:EE:A8:9D:CC": [
    "-64",
    "-80"
],

"C4:A5:DF:24:05:7E": [
    "-65"
],

"79:B8:3B:90:31:12": [
    "-89",
    "-71",
    "-88",
    "-75"
],

"75:8E:FE:9B:69:DA": [
    "-93"
],

"49:BE:B0:74:7A:71": [
    "-66",
    "-82"
],

"55:33:20:52:E1:EC": [
    "-75"
],

"45:D3:80:F3:A4:59": [
    "-79",
    "-71"
],

"44:CA:8A:EF:31:45": [
    "-92",
    "-78"
],

"5F:0D:99:F8:EE:94": [
    "-83"
]

}" }

Thanks in advance to anyone who can help me

0 likes
35 replies
Sinnbeck's avatar

Ok so you got the json data now when you save it to the txt file?

    //file_put_contents('dump.txt', var_dump($request->all()));
furqanDev's avatar

Did you dd in the controller to see if you are getting anything back or not from the API ?

furqanDev's avatar

@Simone94 Then it is something wrong with your other python program or raspberry program. Because you are getting nothing back from that API.

furqanDev's avatar

@Simone94 Make sure to do this

1). Check whether you are sending a post request from that end. If yes then double check the route on which you are sending request to.

2). Check dd using that and also you are in API controller so it is best to return a JSON response back.

public function getDict(Request $request)
{  
		dd($request->all()) ;
    //file_put_contents('dump.txt', var_dump($request->all()));
    $data = json_encode($request);
    DataFromRasp::create($data);

	//
    return response->json([
				'success'=> 'Data successfully store in json format.',
		]);
}
Simone94's avatar

@Sinnbeck I have 500 Internal server error when try to send the Json from raspberry to postbin. I tried with Postman with the same Json and in fact the Json was send

Sinnbeck's avatar

@Simone94 Ok so postman works, and pythons fails? Sounds like a problem with python then

Simone94's avatar

@Sinnbeck if I try to send the Json to Laravel I get:

TypeError: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, string given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 23 in file /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 813

Sinnbeck's avatar

@Simone94 Great. This is how you save it in laravel. Be sure to add the column name to protected $fillable = [] in the model

$data = json_encode($request->get('json'));//json is they key it is sent in
DataFromRasp::create(['column_name' => $data]); //column_name is the column to store the data to
Simone94's avatar

@Sinnbeck using this code, in the column I have null...so is the problem the python code that can't send the json or something else?

Sinnbeck's avatar

@Simone94 Can you try with postman? If that works for postbin, then it should work here too :)

Simone94's avatar

@Sinnbeck Even from postman it returns null. The postman I used is installed on my PC not on the raspberry

Sinnbeck's avatar

@Simone94 That should be fine. How it set up (postman)? Can you perhaps post a screenshot?

And how does the table look ? And the DataFromRaspmodel look?

Simone94's avatar

@Sinnbeck this is the model:

class DataFromRasp extends Model { use HasFactory;

public $timestamps = false;
protected $primaryKey = 'id';

protected $fillable = ['device'];

}

I am looking for a way to insert a screenshot

Sinnbeck's avatar

@Simone94 Ok so I assume that the table just has two columns, "id" and "device" ?

You can just upload to imgur, and post the link :)

Sinnbeck's avatar

@Simone94 Ah. So you are sending it as raw request data, instead of a key, value pair (form data)

$data = json_encode($request->getContent());
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Just did a quick read up on python

Try this

data = {'jsondata': json_obj}
r = requests.post(ip, json=data)

and in laravel

$data = json_encode($request->get('jsondata'));
Sinnbeck's avatar

@Simone94 You can add a cast when extracting it from the database. Then you get an array of all the data

On the model

protected $casts = ['device' => 'array'];
Simone94's avatar

@Sinnbeck so when i go to work on the json will be enough to treat it as an array, right?

Sinnbeck's avatar

@Simone94 laravel will automatically parse it into an array for you, so you can run foreach etc. Show your blade if you want help

Sinnbeck's avatar

Or consider marking this thread as solved as the issue is solved. The others can help out as well

Simone94's avatar

@Sinnbeck I have the same result even if I added that code in my model

https://imgur.com/a/SzKNP8g

The blade.php is this:

@extends('backend.layouts.app')

@section('content')

<tr>

  <th scope="col">ID</th>

  <th scope="col">Device</th>

</tr>
@foreach ($data as $item)

<tr>

  <th scope="row">{{$item->id}}</th>

  <td>{{$item->device}}</td>

  <td>

    <a href="admin/dict" class="btn btn-primary">Select</a>

  </td>

</tr>

@endforeach

@endsection

Sinnbeck's avatar

@Simone94 like I said I suggest creating a new thread. This one is regarding

Save Json data to Database Laravel

And the problem is no longer regarding that.

Sinnbeck's avatar

Oh btw. I think your json is already json encoded? From reading your python. That means you shouldn't json encode again

$data = $request->getContent();
Sinnbeck's avatar

@Simone94 that's totally fine. It's just that the problem is a new one so others can help out

Sinnbeck's avatar

Did you see my double encoding answer? Be sure to get that fixed. Should still work :)

Simone94's avatar

@Sinnbeck yes I have seen it and it works. There is only one difference: it also inserts the jsondata writing

This is the JSON with the code: $data = json_encode($request->get('jsondata'));

"{\n "48:CE:B8:09:7D:AD": [\n "-91"\n ],\n "70:61:91:66:6B:1E": [\n "-81",\n "-69",\n "-79",\n "-65",\n "-74"\n ],\n "4B:6F:42:53:EB:82": [\n "-84",\n "-73",\n "-81"\n ],\n "77:87:80:27:2B:31": [\n "-83",\n "-70",\n "-82",\n "-71",\n "-79",\n "-71",\n "-79",\n "-71",\n "-80",\n "-72"\n ],\n "43:FD:25:20:5A:7B": [\n "-76",\n "-84",\n "-76",\n "-88",\n "-77",\n "-86"\n ],\n "C1:04:08:03:3C:05": [\n "-73"\n ],\n "7F:D2:44:60:82:BD": [\n "-88",\n "-79",\n "-88"\n ],\n "FE:47:DA:73:90:29": [\n "-55",\n "-43",\n "-59",\n "-35",\n "-46",\n "-38",\n "-49"\n ],\n "62:46:58:C8:A9:9A": [\n "-68",\n "-77",\n "-68"\n ],\n "68:13:E4:00:12:30": [\n "-68",\n "-81"\n ],\n "4E:0D:D6:41:77:FA": [\n "-68",\n "-77",\n "-68",\n "-76",\n "-68",\n "-79",\n "-71"\n ],\n "71:B0:03:A6:17:A5": [\n "-82",\n "-73",\n "-81",\n "-73"\n ],\n "19:F2:64:21:35:B5": [\n "-74",\n "-65",\n "-77",\n "-69",\n "-78",\n "-69"\n ],\n "5C:D5:BA:32:06:AD": [\n "-86"\n ],\n "11:91:3D:57:0E:F0": [\n "-90",\n "-82"\n ],\n "44:08:E7:5C:56:B3": [\n "-89",\n "-80"\n ],\n "58:99:6F:53:65:C8": [\n "-77",\n "-85"\n ],\n "54:3C:61:D8:5B:DD": [\n "-88",\n "-79"\n ],\n "59:02:8C:78:5D:E1": [\n "-85",\n "-73",\n "-82",\n "-69",\n "-84",\n "-75",\n "-83",\n "-71"\n ],\n "64:F0:DC:95:6E:16": [\n "-86",\n "-78"\n ],\n "71:E5:42:BA:19:F4": [\n "-91",\n "-77",\n "-86",\n "-73",\n "-84",\n "-74",\n "-84"\n ],\n "E1:63:14:57:89:25": [\n "-48",\n "-57",\n "-38",\n "-47"\n ],\n "57:7D:E9:6F:06:24": [\n "-85"\n ],\n "56:B2:4B:5B:7E:2E": [\n "-88",\n "-80"\n ],\n "64:3D:02:D4:ED:4E": [\n "-76",\n "-64",\n "-72"\n ],\n "0C:93:8F:E5:C6:2A": [\n "-76",\n "-66"\n ],\n "3F:47:42:77:BD:9A": [\n "-64",\n "-83"\n ],\n "3C:46:5E:20:9A:FE": [\n "-79",\n "-57",\n "-74"\n ],\n "67:58:EE:A8:9D:CC": [\n "-64",\n "-80"\n ],\n "C4:A5:DF:24:05:7E": [\n "-65"\n ],\n "79:B8:3B:90:31:12": [\n "-89",\n "-71",\n "-88",\n "-75"\n ],\n "75:8E:FE:9B:69:DA": [\n "-93"\n ],\n "49:BE:B0:74:7A:71": [\n "-66",\n "-82"\n ],\n "55:33:20:52:E1:EC": [\n "-75"\n ],\n "45:D3:80:F3:A4:59": [\n "-79",\n "-71"\n ],\n "44:CA:8A:EF:31:45": [\n "-92",\n "-78"\n ],\n "5F:0D:99:F8:EE:94": [\n "-83"\n ]\n}"

And this is the JSON with the code: $data = $request->getContent();

{"jsondata": "{\n "48:CE:B8:09:7D:AD": [\n "-91"\n ],\n "70:61:91:66:6B:1E": [\n "-81",\n "-69",\n "-79",\n "-65",\n "-74"\n ],\n "4B:6F:42:53:EB:82": [\n "-84",\n "-73",\n "-81"\n ],\n "77:87:80:27:2B:31": [\n "-83",\n "-70",\n "-82",\n "-71",\n "-79",\n "-71",\n "-79",\n "-71",\n "-80",\n "-72"\n ],\n "43:FD:25:20:5A:7B": [\n "-76",\n "-84",\n "-76",\n "-88",\n "-77",\n "-86"\n ],\n "C1:04:08:03:3C:05": [\n "-73"\n ],\n "7F:D2:44:60:82:BD": [\n "-88",\n "-79",\n "-88"\n ],\n "FE:47:DA:73:90:29": [\n "-55",\n "-43",\n "-59",\n "-35",\n "-46",\n "-38",\n "-49"\n ],\n "62:46:58:C8:A9:9A": [\n "-68",\n "-77",\n "-68"\n ],\n "68:13:E4:00:12:30": [\n "-68",\n "-81"\n ],\n "4E:0D:D6:41:77:FA": [\n "-68",\n "-77",\n "-68",\n "-76",\n "-68",\n "-79",\n "-71"\n ],\n "71:B0:03:A6:17:A5": [\n "-82",\n "-73",\n "-81",\n "-73"\n ],\n "19:F2:64:21:35:B5": [\n "-74",\n "-65",\n "-77",\n "-69",\n "-78",\n "-69"\n ],\n "5C:D5:BA:32:06:AD": [\n "-86"\n ],\n "11:91:3D:57:0E:F0": [\n "-90",\n "-82"\n ],\n "44:08:E7:5C:56:B3": [\n "-89",\n "-80"\n ],\n "58:99:6F:53:65:C8": [\n "-77",\n "-85"\n ],\n "54:3C:61:D8:5B:DD": [\n "-88",\n "-79"\n ],\n "59:02:8C:78:5D:E1": [\n "-85",\n "-73",\n "-82",\n "-69",\n "-84",\n "-75",\n "-83",\n "-71"\n ],\n "64:F0:DC:95:6E:16": [\n "-86",\n "-78"\n ],\n "71:E5:42:BA:19:F4": [\n "-91",\n "-77",\n "-86",\n "-73",\n "-84",\n "-74",\n "-84"\n ],\n "E1:63:14:57:89:25": [\n "-48",\n "-57",\n "-38",\n "-47"\n ],\n "57:7D:E9:6F:06:24": [\n "-85"\n ],\n "56:B2:4B:5B:7E:2E": [\n "-88",\n "-80"\n ],\n "64:3D:02:D4:ED:4E": [\n "-76",\n "-64",\n "-72"\n ],\n "0C:93:8F:E5:C6:2A": [\n "-76",\n "-66"\n ],\n "3F:47:42:77:BD:9A": [\n "-64",\n "-83"\n ],\n "3C:46:5E:20:9A:FE": [\n "-79",\n "-57",\n "-74"\n ],\n "67:58:EE:A8:9D:CC": [\n "-64",\n "-80"\n ],\n "C4:A5:DF:24:05:7E": [\n "-65"\n ],\n "79:B8:3B:90:31:12": [\n "-89",\n "-71",\n "-88",\n "-75"\n ],\n "75:8E:FE:9B:69:DA": [\n "-93"\n ],\n "49:BE:B0:74:7A:71": [\n "-66",\n "-82"\n ],\n "55:33:20:52:E1:EC": [\n "-75"\n ],\n "45:D3:80:F3:A4:59": [\n "-79",\n "-71"\n ],\n "44:CA:8A:EF:31:45": [\n "-92",\n "-78"\n ],\n "5F:0D:99:F8:EE:94": [\n "-83"\n ]\n}"}

Please or to participate in this conversation.