Elequent /照亮Laravel数据库LIKE操作不起作用
问题描述:
我使用Laravel的Illuminate数据库管理器,除了现在使用LIKE
操作以外,其工作非常好。Elequent /照亮Laravel数据库LIKE操作不起作用
我已经试过那些选项,但一无所获:
function initConnection()
{
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => $this->config['host'],
'database' => $this->config['database-name'],
'username' => $this->config['username'],
'password' => $this->config['password'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]);
$capsule->setAsGlobal();
}
和初始化后,我想:
function searchByName($word)
{
return Capsule::table($table)
->get()
->where('name', 'LIKE', '%'.$word.'%')
->first();
}
echo searchByName('John');
我也试过这个选项:
Capsule::table('table_name')
->select('SELECT * FROM table_name WHERE table_name.name LIKE "%John%"');
这也失败了。
我找不到在Laravel中使用所有操作的文档。
答
你不需要像这样在雄辩中定义表格。如果按照命名规则的模式将做到这一点本身或U可以通过在模型中设置表名:
class YourModelName extends Model
{
protected $table = 'your_table_name';
}
现在查询是这样的:
function searchByName($word) {
return Capsule::where('name', 'LIKE', '%'.$word.'%')->get();
}
这将返回多个值,这将匹配字符串。