当我尝试运行查询时出现此错误。
BadMethodCallException 调用未定义的方法Illuminate \ Database \ Query \ Builder :: with()
我的搜索控制器在数据库中查找基于URI http://localhost:8000/search/projects/email_category/foo@foo.com_spel+kod-game+code的特定项目
class SearchController extends BaseController {
//foo.com/search/project/email_category/foo@foo.com-admin@foo.com_spel+kod-game+code
public function index($option, $key, $val){
$keys = explode('_', $key);
$vals = explode('_', $val);
$data = [];
for($i = 0; $i < sizeof($keys); $i++){
$data[$keys[$i]] = explode('-', $vals[$i]);
}
$json = json_encode($data);
echo "<pre>" . $option . ": " . $json . "</pre>";
// Get all rows in option table, but we are going to sort things out before calling it with ->get();
$table = DB::table($option);
foreach($data as $key => $value){
foreach($value as $string){
$split = explode('+', $string);
foreach ($split as $explode){
if(end($split) !== $explode){
switch($key){
case 'email':
$table->with(array('User' => function($query) use ($key, $explode){
$query->where('email',"=",$explode);
}));
break;
case 'categories':
$table->Category->where('typ',"=",$explode);
break;
default:
$table->where($key,"=",$explode);
break;
}
}
}
switch($key){
case 'email':
$table->with(array('User' => function($query) use ($explode){
$query->orWhere('email',"=",$explode);
}));
break;
case 'categories':
$table->Category->orWhere('typ',"=",$explode);
break;
default:
$table->orWhere($key,"=",$explode);
break;
}
}
}
$table->get(); // Runs the query we have build up.
}
}
用户模型
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function project()
{
return $this->hasMany('Project');
}
public function experience()
{
return $this->hasMany('Experience');
}
public function reference()
{
return $this->hasMany('Reference');
}
public function stat()
{
return $this->hasMany('Stat');
}
}
项目模型
<?php
class Project extends Eloquent
{
protected $table = 'projects';
public function user()
{
return $this->belongsToMany('User');
}
public function category()
{
return $this->hasMany('Category');
}
public function stat()
{
return $this->hasMany('Stat');
}
}
我试图关注Laravels网页上的一些例子。 我做了一个测试字符串查询,显示正确的字符串,但现在我需要它在ORM中。我已经尝试过我自己解决这个问题已经有好几天了,之前我从来没有在Stackoverflow中发布任何内容,如果你发现一些不清楚的评论,我会添加它。
答案 0 :(得分:0)
我不知道这是否能解决所有事情,但是对于初学者而言,使用的是模型而不是DB::table()
:
// ...
$model = studly_case($option);
$table = new $model;
// ...