-1

The SQL just like this:

select a,b,concat(a,b) from table

But we don't want to use raw sql(whereRaw() and so on), we want to use ORM and the Query Constructor.

zx8754
  • 52,746
  • 12
  • 114
  • 209

2 Answers2

0

You can do like this:
1. Attribute mode: In your model
Add

protected $appends = ['con_cat'];

public function getConCatAttribute()
{
    return $this->attributes['A'].$this->attributes['B']
}

Now you can use

$yourModel->con_cat

2. Function mode: In your model

public function concatAB()
{
    return $this->attributes['A'].$this->attributes['B']
}

Now you can use

$yourModel->concatAB()
KmasterYC
  • 2,294
  • 11
  • 19
0

You can use Raw Expression for concat in ORM and the Query Constructor. Like below :

Model::select('a','b',\DB::raw("concat(a,' ',b)"))
       ->get();
Kiran Subedi
  • 2,244
  • 3
  • 17
  • 34
  • yeah,but is there any way we can express that without raw expression? May be use other query constructors? Sorry My boss is an obsessive and throw this mad question and I can't handle it... – 许立斌 Aug 16 '16 at 11:15