Cloud Stack Ninja

Sorry Im new to Laravel and trying to save to the database for the first time. Im trying to save an array to the database but the error "array to string conversion" is appearing. I've tried changing the string value in the migration file to other options but the same error is appearing.

My form is like this

socials

I have 3 migrations for tables

socials table

public function up()
{
    Schema::create('socials', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });
}

users

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('image')->nullable();
        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->string('mobile')->nullable();
        $table->timestamps();
    });
}

social_networks

public function up()
{
    Schema::create('social_networks', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('related_id');
        $table->timestamps();
    });

    Schema::create('social_user', function (Blueprint $table) {
        $table->bigInteger('social_id')->unsigned();
        $table->foreign('social_id')->references('id')->on('socials')->onDelete('cascade');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->primary(['social_id' , 'user_id']);
    });
}

When saving in controller

public function update(Request $request)
{
    $user = Auth::user();
    $user->image = $path;
    $user->first_name = $request->first_name;
    $user->last_name = $request->last_name;
    $user->save();
    foreach ($request->related_id as $related) {
        $socialNetwork = new SocialNetwork();
        $socialNetwork->user_id = auth()->id();
        $socialNetwork->related_id = $request->related_id;
        $socialNetwork->social_id = $request->social_id;
        $socialNetwork->save();
        $user->socials()->sync($socialNetwork->id);
    }
}

I see this error

Array to string conversion



Read more here: https://stackoverflow.com/questions/64190275/how-to-save-social-network

Content Attribution

This content was originally published by Mahmoud Khosravi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: