我正在尝试使用观察者进行关系删除,但是问题是,当我在创建函数中的DD正常工作时,但是当我在删除函数中的DD却不显示任何东西时(POSTMAN)表示工作或错误均不相同
这里是Api:
$api->post('store','App\Http\Controllers\CustomerController@store');
$api->delete('delete/{id}','App\Http\Controllers\CustomerController@destroy');
这是工匠制作的观察者文件
namespace App\Observers;
use App\Customer;
class CustomerObserver
{
public function created(Customer $customer)
{
dd($customer);
}
public function deleted(Customer $customer)
{
dd($customer);
}
}
这是客户总监
class CustomerController extends Controller
{
public function store(Request $request)
{
return Customer::store($request->person);
}
public function destroy($id)
{
$delete = Customer::where('person_id',$id);
$delete->delete();
}
}
这是客户模型文件。
class Customer extends Model
{
//Relationship Start From Here
public function person()
{
return $this->belongsTo(Person::class);
}
//End Here
public static function store($request)
{
//Call to Person Model And Save User
$user = Person::store($request);
//Create object of Customer Model
$customer = new Customer();
$customer->fill($request['customers']);
$customer->person()->associate($user)->save();
//return customer
return $customer;
}
}
答案 0 :(得分:1)
我知道这可能是一个迟到的回复,并且不确定您是否还在寻找答案。我认为问题在于您如何删除 Customer
模型。
当你做类似的事情时
$delete = Customer::where('person_id',$id);
$delete->delete();
您正在执行批量删除语句。如 laravel document 中所述,mass deletes will not fire any model events for the models that are deleted
这就是您的 deleted
观察者事件没有触发的原因。
通过 Eloquent 执行批量删除语句时,不会为已删除的模型触发删除和删除模型事件。这是因为在执行删除语句时实际上从未检索到模型
现在看看如何创建 Customer
。您一次创建一个模型。因此,您的 created
观察者确实可以运行。
//Create object of Customer Model
$customer = new Customer();
$customer->fill($request['customers']);
$customer->person()->associate($user)->save();
解决你的问题,最简单的方法就是把所有的模型都找回来,一一删除,这样你就可以触发事件了。
foreach (Customer::where('person_id',$id)->get() as $delete) {
$delete->delete();
}
答案 1 :(得分:0)
你能做所有事情吗
在#include <iostream>
#include <string>
#include <vector>
using namespace std;
class rolodex
{
string name;
string street, town, state;
string zip;
string phone;
vector <rolodex> entries;
public:
rolodex();
void getmenu();
void add_entry();
void set_name();
void set_address();
void set_phone();
void printinfo();
};
rolodex :: rolodex() : name(""), street(""), town(""), state(""), zip(""),
phone(""), entries()
{
}
void rolodex :: getmenu()
{
cout << "\n\n1)Add Entry";
cout << "\n5)Print All Entries";
cout << "\n6)Exit" << endl;
}
void rolodex :: add_entry()
{
rolodex temp;
cout << "\n\nEnter Name: ";
temp.set_name();
temp.set_address();
cout << "\n\nEnter Your Phone Number: ";
temp.set_phone();
entries.push_back(temp);
}
void rolodex :: set_name()
{
cin.ignore();
getline(cin, name);
}
void rolodex :: set_address()
{
cout << "\n\nNow we'll enter address information.";
cout << "\n\nStreet: ";
getline(cin, street);
cout << "\n\nTown: ";
getline(cin, town);
cout << "\n\nState: ";
getline(cin, state);
cout << "\n\nZip: ";
getline(cin, zip);
}
void rolodex :: set_phone()
{
getline(cin, phone);
}
void rolodex :: printinfo()
{
for(unsigned int i = 0; i < entries.size(); i++)
{
cout << entries[i] << endl; //This is where I'm stuck since I've only
//worked with vectors of non-object data
//type
}
}
int main()
{
rolodex person, menu;
short choice;
bool done = false;
do
{
menu.getmenu();
cout << "\n\nEnter a choice: ";
cin >> choice;
switch(choice)
{
case 1:
person.add_entry();
break;
case 5:
person.printinfo();
break;
case 6:
done = true;
break;
default:
cout << "\n\nInvalid Entry." << endl << endl;
}
} while(!done && isdigit(choice));
return 0;
}
方法的CustomerServiceProvider中的Customer::observe(CustomerObserver::class);
中添加1行
在提供程序数组的app.php文件中添加boot
CustomerServiceProvider
composer dump-autoload
答案 2 :(得分:0)
我也遇到这个问题,但是我从存储库中调用delete方法,这阻止了Observer的delete事件被触发。当我从模式使用删除功能时,它可以正常工作。