I have been using the Laravel package for the Wordpress JSON REST API and it works well. I want to store my blogposts in my database. I used phpMyAdmin
to add a table named idybrand_laravel
I created a file app/blogpost
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class blogpost extends Model
{
//
}
and for now in my BlogController
I am trying a simple test, wishing to store data in just 3 fields of the table
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use WpApi;
use App\blogpost;
use Carbon\Carbon;
class BlogController extends Controller
{
public function getPosts()
{
foreach (WpApi::posts() as $post) {
$this->createPost($post);
}
//return view('pages.blog', ['active'=>'navBlog'])->with('posts', WpApi::posts());
}
protected function createPost($data)
{
$post = new blogpost();
$post->id = $post['id']; // integer
$post->wp_id = $post['id']; // integer
$post->link = $post['link']; //string
$post->save();
return $post;
}
}
I have not changed config/database.php
and my .env
file looks like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=idybrand_laravel
DB_USERNAME=idybrand_dave
DB_PASSWORD=myPassword
The error I receive is:
QueryException in Connection.php line 770:
SQLSTATE[HY000] [2002] Connection refused (SQL: insert into
blogposts(
id,
wp_id,
link,
updated_at,
created_at) values (, , , 2017-02-17 10:21:57, 2017-02-17 10:21:57))
Do I need to write some code like init
or save
inside app/blogpost
? Can anybody tell me why the connection is refused? I'm using localhost
as my project is in development not production.
EDIT:
After some help from the guys in chat including davejal, I am now able to connect to the server BOTH via the command line in OS X:
$ mysql -u idybrand_dave -pMY_PASSWORD -h 181.224.130.159 idybrand_laravel
AND using Sequel Pro with no problem. But I still get the same error in Laravel when trying to connect using those exact same settings in my .env
file:
DB_CONNECTION=mysql
DB_HOST=181.224.130.159 //Siteground IP Address
DB_PORT=3306
DB_DATABASE=idybrand_laravel
DB_USERNAME=idybrand_dave
DB_PASSWORD=MY_PASSWORD
I also tried to edit my model as suggested by Sona but it doesn't help:
class blogpost extends Model
{
protected $table = 'blogposts';
protected $fillable = array('id', 'wp_id', 'link', 'title', 'author_code', 'content', 'created_at', 'updated_at');
}
Can anybody please shed any light on this error?
FINAL EDIT:
I had a fundamental misunderstanding in that I thought I could use a localhost 'local' development environment to write data to a table that exists on the server (Siteground) in the 'production' environment.
Once I read the Laravel Database Getting Started and watched these three videos by Mindspace all became clear. Basically, I needed a server environment running locally and this is provided by Vagrant and VMWare Fusion.
After setting it up I could access http://idy.app/
in the browser whilst the homestead-7 VM
was running and write to the database.
My .env
file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=33060
DB_DATABASE=idy
DB_USERNAME=homestead
DB_PASSWORD=secret
I have accepted davejal's answer as it lead me to the solution.