我正在努力在拉腊维尔保存许多人。我已经试了将近7个小时了。但是仍然不能解决这个问题。
下面是自定义迁移表:
public function up()
{
Schema::create('customer', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string("customer_phone", 255);
});
}
下面是我的send_messages迁移表:
public function up()
{
Schema::create('send_messages', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger("template_id");
$table->unsignedBigInteger("sender_number_id");
// $table->unsignedBigInteger("cutomer_id");
$table->foreign("template_id")->references("id")->on("messagecontent")->onDelete("cascade");
$table->foreign("sender_number_id")->references("id")->on("sendernumber")->onDelete("cascade");
// $table->foreign("customer_id")->references("id")->on("customer")->onDelete("cascade");
});
}
下面是我的数据透视表:
Schema::create('customer_send_message', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId("send_messages_id")->constrained("send_messages");
$table->foreignId("customer_id")->constrained("customer");
});
这里是客户模型
class Customer extends Model
{
use HasFactory;
protected $table = "customer";
protected $fillable = [ "customer_phone" ];
public function sendmessage () {
return $this->belongsToMany(SendMessage::class, "customer_send_message");
}
}
这是我的发送消息模型
class SendMessage extends Model
{
use HasFactory;
protected $table = "send_messages";
protected $fillable = [
"customer_id",
"template_id",
"sender_number_id"
];
public function messageContent() {
return $this->belongsTo(MessageContentModel::class);
}
public function senderNumber() {
return $this->belongsTo(SendNumberModel::class);
}
public function customer() {
return $this->belongsToMany(Customer::class, "customer_send_message");
}
}
下面是我尝试保存的控制器:
public function send(Request $request) {
$sendmessage = new SendMessage();
$sendmessage->template_id = $request->get("template_id");
$sendmessage->sender_number_id = $request->get("sender_number_id");
$customers = Customer::find(1);
$sendmessage->customer()->attach($customers);
$sendmessage->save();
// $send->save();
return redirect("")->with("message", "Sms sent!");
}
我想得到多个数据,然后我想保存它就像django manytomanyfield.实际上我是一个django开发者,我最近切换到laravel.我不知道这么多的事情.所以有什么解决这个问题的方法吗?我只想保存.
保存时出现错误:
状态[42 S22]:未找到列:1054“字段清单”中有不明的数据行“send_message_id”
我不知道为什么我得到这个错误!!
1条答案
按热度按时间p4tfgftt1#
由于laravel的“约定优先于配置”策略,它倾向于对列名做一些假设。如果列名与这些假设不匹配,您就会遇到这些问题。IIRC,这里的问题是您的类名是
SendMessage
,Laravel将从中推断出数据透视表上的外键是send_message_id
,但实际上是send_messages_id
。您应该可以通过更改
SendMessage
中的关系定义来解决此问题: