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()));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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'));
Please or to participate in this conversation.