0
我在laravel中遇到以下錯誤: 有2個表用戶和軌道與多對多的關係和中間表名稱是track_user我想刪除但無法記錄做無法刪除記錄中的一對多關係laravel雄辯
Unhandled Exception
Message:
Call to a member function track() on a non-object
Location:
C:\wamp\www\integron\application\controllers\track.php on line 50
MySQL錯誤:
SQL query:
DELETE FROM `integron`.`tracks` WHERE `tracks`.`id` =2
MySQL said:
#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`integron`.`track_user`, CONSTRAINT `track_user_project_id_foreign` FOREIGN KEY (`track_id`) REFERENCES `tracks` (`id`))
用戶模型:
<?php
class User extends Eloquent{
public static $table = 'users';
public function tracks()
{
return $this->has_many_and_belongs_to('Track');
}
軌道型號:
<?php
class Track extends Eloquent{
public static $table = 'tracks';
public function users()
{
return $this->has_many_and_belongs_to('User');
}
控制器
//this funciton is working properly
public function post_trackUpdate($id){
$track = Track::find($id);
$track->name = Input::get('name');
$track->description = Input::get('desc');
$track->save();
}
//this funciton is not working properly
public function get_trackDelete($id){
$track = Track::find($id);
$track->delete();
}
..
//an alternate which i tried but didnt work either
public function get_trackDelete($id){
$user=User::find($id);
$user->tracks()->delete();
Session::flash('result','track Details deleted');
return Redirect::to('track');
}
我怎樣才能在複合表中刪除記錄? –