25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Loading the mock data<br />

<strong>Laravel</strong> provides a File facade that allows us to open files from disk as simply as<br />

File::get($path). To get the full path to our mock data file we can use the<br />

base_path() helper function, which returns the path to the root of our application<br />

directory as a string.<br />

It's then trivial to convert this JSON file to a PHP array using the built-in<br />

<strong>js</strong>on_decode method. Once the data is an array, it can be directly inserted into the<br />

database given that the column names of the table are the same as the array keys.<br />

database/ListingsTableSeeder.php:<br />

public function run()<br />

{<br />

$path = base_path() . '/database/data.<strong>js</strong>on';<br />

$file = File::get($path);<br />

$data = <strong>js</strong>on_decode($file, true);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!