2017-08-01 63 views
1

我使用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中使用所有操作的文檔。

回答

0

你不需要像這樣在雄辯中定義表格。如果按照命名規則的模式將做到這一點本身或U可以通過在模型中設置表名:

class YourModelName extends Model 
{ 
    protected $table = 'your_table_name'; 
} 

現在查詢是這樣的:

function searchByName($word) { 
    return Capsule::where('name', 'LIKE', '%'.$word.'%')->get(); 
} 

這將返回多個值,這將匹配字符串。