多对多关系中的条件分离 laravel
Conditional detach in many to many relationship laravel
我有2个模型Tour.php
public function includes()
{
return $this->belongsToMany('App\Included');
}
Included.php
public function tours()
{
return $this->belongsToMany('App\Tour');
}
如果 tour
中有 includeds
,我正在尝试分离 includeds
,同时删除游览。
下面的代码是我试过的:
public function destroy($id)
{
$tour = Tour::find($id);
if ($test = $tour->includeds()->count() != null) {
$tour->includeds()->detach();
}
if ($test = $tour->excludeds()->count() != null) {
$tour->excludeds()->detach();
}
$tour->delete();
Session::flash('success', 'The tour is sucessfully deleted.');
return redirect()->route('tours.index');
}
以上代码生成 Call to undefined method Illuminate\Database\Query\Builder::includeds()
错误。请指出我犯的错误。
小错别字
试试下面的代码
public function destroy($id)
{
$tour = Tour::find($id);
if ($test = $tour->includes()->count() != null) {
$tour->includes()->detach();
}
if ($test = $tour->excludeds()->count() != null) {
$tour->excludeds()->detach();
}
$tour->delete();
Session::flash('success', 'The tour is successfully deleted.');
return redirect()->route('tours.index');
}
我有2个模型Tour.php
public function includes()
{
return $this->belongsToMany('App\Included');
}
Included.php
public function tours()
{
return $this->belongsToMany('App\Tour');
}
如果 tour
中有 includeds
,我正在尝试分离 includeds
,同时删除游览。
下面的代码是我试过的:
public function destroy($id)
{
$tour = Tour::find($id);
if ($test = $tour->includeds()->count() != null) {
$tour->includeds()->detach();
}
if ($test = $tour->excludeds()->count() != null) {
$tour->excludeds()->detach();
}
$tour->delete();
Session::flash('success', 'The tour is sucessfully deleted.');
return redirect()->route('tours.index');
}
以上代码生成 Call to undefined method Illuminate\Database\Query\Builder::includeds()
错误。请指出我犯的错误。
小错别字
试试下面的代码
public function destroy($id)
{
$tour = Tour::find($id);
if ($test = $tour->includes()->count() != null) {
$tour->includes()->detach();
}
if ($test = $tour->excludeds()->count() != null) {
$tour->excludeds()->detach();
}
$tour->delete();
Session::flash('success', 'The tour is successfully deleted.');
return redirect()->route('tours.index');
}