Home > Software engineering >  How to save manytomany in laravel
How to save manytomany in laravel

Time:12-05

I am trying to save manyto many in laravel. I have been trying for almost 7 hours. But still can't solve this issue.

Here is custom migration table:

public function up()
    {
        Schema::create('customer', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string("customer_phone", 255);
        });
    }

Here is my send_messages migration table:

 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");

        });
    }

Here is my pivot table:

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");
});

Here is CustomerModel

class Customer extends Model
{
    use HasFactory;

    protected $table = "customer";

    protected $fillable = [ "customer_phone" ];

    public function sendmessage () {
        return $this->belongsToMany(SendMessage::class, "customer_send_message");
    }

}

Here is my SendMessageModel

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");
    }

}

Here is my controller I am trying to save:

    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!");
    }

I want to get multiple data then I want to save it just like django manytomanyfield. Acutally I was a django developer I recently switched to laravel. I don't know so many things. So Is there any solution for solving this issue? I just want to save.

I am getting an error while saving :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'send_message_id' in 'field list'

I don't know why I am getting this error!!

CodePudding user response:

Due to laravel's 'convention over configuration' policy, it tends to make some assumptions regarding your column names. If the column names don't match those assumptions, you run into these kind of issues. IIRC, the problem here is that your class name is SendMessage, from which Laravel will deduce the foreign key on your pivot table is send_message_id, it is however send_messages_id.

You should be able to fix this by changing your relation definition in SendMessage:

public function customer() {
  return $this->belongsToMany(
    Customer::class, 
    'customer_send_message', 
    'send_messages_id'
  );
}
  • Related