0
嘿家伙即时通讯新的laravel和我试图插入我的数据透视表。我有这样的结构,我的数据库laravel 4中的数据透视表插入
部门表属于多个类别和相同类别,所以我有这个型号
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Departments extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'departments';
protected $fillable = ['department_name'];
public $timestamps = false;
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
public function categories()
{
return $this->belongsToMany('Categories');
}
}
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Categories extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'categories';
protected $fillable = ['name'];
public $timestamps = false;
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
public function department()
{
return $this->belongsToMany('Departments');
}
}
然后我有这样
在我的控制器查询$messages = array(
'required' => 'Please Fill the required field',
'unique' => 'Name Already exist'
);
$catName = Input::get('categoryName');
$deptId = Input::get('deptId');
$validation = Validator::make(Input::all(),[
'categoryName' => 'required|unique:categories,name' ], $messages);
if($validation->fails()){
return array('error' =>$validation->messages()->all());
}else{
$findDepartment = Departments::find($deptId);
$saveCat = $findDepartment->categories()->insert(array('name' => $catName));
}
但是当我检查表时,它加在分类表上,但没有在category_department中添加。我想念任何代码吗?还有我上次尝试迁移我的数据透视表时出错,错误是这样的。
你能帮助我在我缺少什么人? tnx的高级帮助。
如果我想要插入一个具有相同类别的多个部门?我将如何在单个查询中做到这一点? – Aoi
然后你可以使用sync $ findDepartment-> categories() - > sync(array(1,2,3)); – angoru
嗯..我试图让一个用户有多个部门这是我做的http://pastie.org/8969588,但后来我有这个错误:非法胶印类型 – Aoi