Zach Tackett
Zach Tackett

Reputation: 614

How to load json file into Laravel Controller?

I have a JSON file that I would like to load into a controller in Laravel so that I can use the data into my application.

The files path is /storage/app/calendar_Ids.json.

What is the correct way to go about doing this?

Upvotes: 9

Views: 29133

Answers (4)

Pankaj Bawliya
Pankaj Bawliya

Reputation: 11

You can try this too:

 $json = file_get_contents('your json file path here.....');

//converting json object to php associative array
$data = json_decode($json, true);

foreach($data as $item){

   
        DB::table('table name')->insert(
            array(
                    
                   'clientId'     =>   $item['clientId'], 
                   'socialMediaLinkId'   =>   $item['socialMediaLinkId'],
                   'link'   =>   $item['link'],
                   'isEnabled'   =>   $item['isEnabled'],
                   'created_at'   =>   $item['created_at'],
                   'updated_at'   =>   $item['updated_at'],
                   'isSelectedByDefault'   =>$item['isSelectedByDefault'],
                   'showOnNegativePage'   =>$item['showOnNegativePage']
        
        
            )
        );
    }

Upvotes: 1

Yannick Dirbé
Yannick Dirbé

Reputation: 72

You can try this too:

$json = storage_path('folder/filename.json');

Upvotes: -1

Mike Barwick
Mike Barwick

Reputation: 5367

Here, this should help you get sorted.

use Storage;

$json = Storage::disk('local')->get('calendar_Ids.json');
$json = json_decode($json, true);

Upvotes: 21

Onix
Onix

Reputation: 2179

Try this

$path = '/storage/app/calendar_Ids.json';
$content = json_decode(file_get_contents($path), true);`

Then just dd($content); to see if it works.

Upvotes: 5

Related Questions