不需要的前缀被添加到查询的列名称中



就像标题所说,我在查询和 Laravel 方面遇到了问题。由于某些原因,前缀"dev_"被添加到我的列名中(只有列名,而不是表或其他)。 这当然会导致以下错误,因为该列没有"dev_"前缀

列名错误

该问题发生在运行Apache和Laravel5.0.18的Ubuntu服务器上。 我设置了它,以便它处理多个数据库(一个数据库用于生产,一个数据库用于开发)。 这是我的配置/数据库的连接.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,
],
]
...

生产数据库(mysql)和开发数据库(mysqldev)在表,列等方面是相同的。 我通过雄辩的模型在我的 api 中使用它们(每次一个模型用于生产,一个模型用于开发)我已经为我的开发 api 设置了一个路由组前缀,该前缀与生产 API 具有相同的端点,但使用的是开发模型。 它对于生产 API 来说工作得很好,但在开发 API 上发生了上述问题。 这是我的模型, 用户:

<?php namespace pgc;
use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
public function saves()
{
return $this->hasMany('pgcSave');
}
public function savespgc()
{
return $this->hasMany('pgcSave')->where('bya','=','0');
}
public function savesbya()
{
return $this->hasMany('pgcSave')->where('bya','=','1')->orderby('name','ASC');
}

public function screenshots()
{
return $this->hasMany('pgcScreenshot');
}
public function screenshotsbya()
{
return $this->hasMany('pgcScreenshot')->where('bya','=','1')->where('hidden','=','0');
}
public function screenshotspgc()
{
return $this->hasMany('pgcScreenshot')->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'];
}

开发用户 :

<?php namespace pgc;
use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
class DevUser extends Model implements AuthenticatableContract, CanResetPasswordContract {
public function saves()
{
return $this->hasMany('pgcDevSave');
}
public function savespgc()
{
return $this->hasMany('pgcDevSave')->where('bya','=','0');
}
public function savesbya()
{
return $this->hasMany('pgcDevSave')->where('bya','=','1')->orderby('name','ASC');
}

public function screenshots()
{
return $this->hasMany('pgcDevScreenshot');
}
public function screenshotsbya()
{
return $this->hasMany('pgcDevScreenshot')->where('bya','=','1')->where('hidden','=','0');
}
public function screenshotspgc()
{
return $this->hasMany('pgcDevScreenshot')->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'];
}

救:

<?php namespace pgc;
use IlluminateDatabaseEloquentModel;

class Save extends Model {
public function user()
{
return $this->belongsTo('pgcUser');
}
/**
* 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'];
}

开发保存 :

<?php namespace pgc;
use IlluminateDatabaseEloquentModel;

class DevSave extends Model {
public function user()
{
return $this->belongsTo('pgcDevUser');
}
/**
* 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'];
}

这就是开发路由组前缀的设置方式:

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 = pgcDevUser::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();
});
...
});

对于生产端,它是相同的后端点函数,但改用用户模型。(就像我上面说的,它在生产方面工作正常)。

您的模型名称是DevUser,但您在数据库中使用了user_id。如果您没有明确指定用于关系的列,Laravel将尝试猜测它,这就是dev_user_id的来源。按如下方式定义您的关系:

DevSave.php

public function user(){
return $this->belongsTo('pgcDevUser', 'user_id');
}

或者考虑在您的关系名称中使用dev,例如public function devUser()等。

你的代码在生产中工作的原因是你使用的是User,而不是DevUser,所以Laravel正确地猜测user_id是你想要在关系中使用的列。

最新更新