Viewed   162 times

I am receiving the following error on my localhost for Laravel 4.1 (using MAMP)

SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61)

It points to:

/Applications/MAMP/htdocs/crowdsets/laravel-master/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php

This is the function it is pointing to:

public function createConnection($dsn, array $config, array $options)
    {
        $username = array_get($config, 'username');

        $password = array_get($config, 'password');

        return new PDO($dsn, $username, $password, $options);
    }

Up to this point, I had not received this error.

I have a local environment and production environment set up (the default).

in config/local/database.php I have:

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'database',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

 Answers

4

An error like that means the server itself is not even reachable. Did you start MySQL in MAMP?

Also, how have you started MAMP? With the standard MySQL 3306 port? Or the alternative port MAMP uses for non-admins: 8889?

I bet your server is running, but is attempting to connect to 3306 so you need to set the port to 8889. Change your config to be like this; note the addition of the port param:

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'port'      => '8889',
            'database'  => 'database',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

EDIT: I just found this question thread that addresses the issue of connecting Laravel to MAMP via port 8889.

Monday, November 14, 2022
 
skar
 
5

Never do anything more than loop thru your data in your view layer. Basically a normal MVC pattern in laravel could be something like this:

It all begins with the routing layer (which btw. is fantastic in laravel)

Using closures

Route::get('/home', function()
{

 //Here data is an array, normally you would fetch data 
 //from your database here and pass it to the View.

 $data = array('this', 'is', 'my', 'data-array');
 return View::make('my.view')->with(compact('data');

});

Using controllers (and a controller method)

//the same route is bound to a controller method
Route::get('/home','HomeController@myFunction');

The controller for the above could look something like this:

<?php

class HomeController extends BaseController {

  //The function you call from your route
  public function myFunction()
  {

     $data = array('this', 'is', 'my', 'data-array');
     return View::make('my.view')->with(compact('data');

  }

}

The above example just shows the VC in MVC, but generally you pass data from your models in the same way.

Heres a quick one:

Model usage in controllers

  public function myFunction($user)
  {

     $userdata = User::find($user)->orderBy('firstname', 'desc');
     $infodata = Event::find(1)->course;
     return View::make('my.view')->with(compact('data', 'infodata');

  }

So the idea is that laravel lets you do things in multiple ways. If you have a minor app, and are sure that you can manage without controllers you could skip the controller and keep everything in your routing layer.

For most apps however the controllers are needed for controlling the dataflow in the application.

If you are totally new to MVC you should check out some tuts on the subject.

EDIT:

Ahaa! So you wanted to share some piece of data in all your views! Well thats simple. Because all your controllers extend the BaseController you can simply pass in the data in there. Like so:

    class BaseController extends Controller {

      public function __construct()
      { 
        $data = array('alot', 'of', 'data');
        return View::share('data', $data);
      }
   }

Now the data variable is available in all the views.

PS. Filters are meant to filter stuff, like to check if certain things are "ok". This could include to check authed users, or form submissions etc.

Monday, November 7, 2022
4

Try This

$users = DB::table('flowers')
->select(["id", "name",
      DB::raw("
       case 
          when visibility_status = '1' 
          then 'Visible' 
          when visibility_status = '0' 
          then 'Invisible'
          end as visibility_status
    ")])->get();

Here is the reference for it http://laravel.com/docs/4.2/queries#raw-expressions

Sunday, October 23, 2022
2

Go to System preferences -> MySql and check the state of your MySql instance.

Monday, August 29, 2022
 
2

Well, I don't exactly know what caused this issue in the first place, but I was able to find a way to solve it. A helpful, if old - perfect in this case - tutorial I found on the web, that hopefully can help others with the same problem.

Tutorial: Install MySQL 4.0 - Windows, by Dr. Thomas E. Hicks Computer Science Department Trinity University

Link Here

Saturday, October 15, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :