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

时间:2019-04-24 16:50:19

标签: php laravel laravel-5

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

Column names error

该问题发生在运行Apache和Laravel 5.0.18的Ubuntu服务器上。 我将其设置为可以处理多个数据库(一个数据库用于生产,一个数据库用于开发)。 这是我的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,
        ],

    ]

...

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

<?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'];

}

保存:

<?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'];

}

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

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();
    });

    ...

});

对于生产端,它是相同的post终结点函数,但使用的是User模型。 (就像我上面说的,它在生产方面可以正常工作。)

1 个答案:

答案 0 :(得分:2)

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

DevSave.php中:

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

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

您的代码在生产环境中起作用的原因是您使用的是User,而不是DevUser,因此Laravel正确地猜测user_id是您要在关系中使用的列