Like the title says, I am having issues with queries and Laravel. For some reasons the prefix "dev_" is added to my column names (Only the column names, not the table or else). It of course result in the following error because the column do not have the "dev_" prefix
The issue is happening on a Ubuntu server running Apache and Laravel 5.0.18. I set it up so that it handles multiple Databases (One database for production and one for development). Here are the connections of my config/database.php
...
'default' => 'mysql',
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path().'/database.sqlite',
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysqldev' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DEV_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
]
...
The production database (mysql) and the dev database (mysqldev) are identical in terms of table, column etc... I'm using them in my api through eloquent models (One model for prod and one model for dev each time) I've set up a route group prefix for my dev api which has the sames endpoint as the prod api but is using the dev models. And it is working perfectly fine for the prod API but on the dev API the issue described above happen. Here are my models, User :
<?php namespace pgc;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
public function saves()
{
return $this->hasMany('pgc\Save');
}
public function savespgc()
{
return $this->hasMany('pgc\Save')->where('bya','=','0');
}
public function savesbya()
{
return $this->hasMany('pgc\Save')->where('bya','=','1')->orderby('name','ASC');
}
public function screenshots()
{
return $this->hasMany('pgc\Screenshot');
}
public function screenshotsbya()
{
return $this->hasMany('pgc\Screenshot')->where('bya','=','1')->where('hidden','=','0');
}
public function screenshotspgc()
{
return $this->hasMany('pgc\Screenshot')->where('bya','=','0')->where('hidden','=','0');
}
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['user_id' , 'duid' , 'name', 'email', 'password','dealer'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token','duid','updated_at','created_at'];
}
DevUser :
<?php namespace pgc;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class DevUser extends Model implements AuthenticatableContract, CanResetPasswordContract {
public function saves()
{
return $this->hasMany('pgc\DevSave');
}
public function savespgc()
{
return $this->hasMany('pgc\DevSave')->where('bya','=','0');
}
public function savesbya()
{
return $this->hasMany('pgc\DevSave')->where('bya','=','1')->orderby('name','ASC');
}
public function screenshots()
{
return $this->hasMany('pgc\DevScreenshot');
}
public function screenshotsbya()
{
return $this->hasMany('pgc\DevScreenshot')->where('bya','=','1')->where('hidden','=','0');
}
public function screenshotspgc()
{
return $this->hasMany('pgc\DevScreenshot')->where('bya','=','0')->where('hidden','=','0');
}
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $connection = 'mysqldev';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['user_id' , 'duid' , 'name', 'email', 'password','dealer'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token','duid','updated_at','created_at'];
}
Save :
<?php namespace pgc;
use Illuminate\Database\Eloquent\Model;
class Save extends Model {
public function user()
{
return $this->belongsTo('pgc\User');
}
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'saves';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['user_id', 'name', 'savedata'];
}
DevSave :
<?php namespace pgc;
use Illuminate\Database\Eloquent\Model;
class DevSave extends Model {
public function user()
{
return $this->belongsTo('pgc\DevUser');
}
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'saves';
protected $connection = 'mysqldev';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['user_id', 'name', 'savedata'];
}
And this is how the dev route group prefix is set up :
Route::group(['prefix' => 'devs'], function ()
{
...
Route::post('getpreconfig', function()
{
$bya = false;
if (!is_null(Input::get('type')))
{
if (Input::get('type') == "bya")
$bya = true;
}
$user = pgc\DevUser::where('name', '=', "admin")->first();
if(is_null($user))
return ("error:user not found or logged in!");
if ($bya)
$allsaves = $user->savesbya;
else
$allsaves = $user->savespgc;
if (is_null($allsaves))
return ("empty");
//echo ($allsaves);
return $allsaves->toJson();
});
...
}
For the production side, it is the same post endpoint function but using the User model instead. (And like I said above it is working fine for the production side).
from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2Pt8cIu
via IFTTT
Aucun commentaire:
Enregistrer un commentaire