Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

How to save social network?

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

0 likes
7 replies
shahr's avatar
Level 10

my blade

<form id="basic-information" method="post" enctype="multipart/form-data" action="{{ route('update', auth()->id()) }}">
    @csrf
    @method('put')
    <div class="row bg-white p-3 mb-5">
        <div class="col-md-2">
            <input name="image" onchange="previewFile()" type="file" class="d-none">
            @if (auth()->check() && auth()->user()->image)
                <img src="{{ asset('images/users/'.auth()->user()->image) }}"  id="dialog" class="img-fluid rounded-circle cursor-pointer">
            @else
                <img src="{{ asset('images/avatar.png') }}" id="dialog" class="img-fluid cursor-pointer">
            @endif
        </div>
        <div class="col-md-10">
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="first_name">first_name</label>
                        <input type="text" class="form-control" value="{{ auth()->user()->first_name }}" id="first_name" name="first_name">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="last_name">last_name</label>
                        <input type="text" class="form-control" value="{{ auth()->user()->last_name }}" id="last_name" name="last_name">
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="form-group">
                        <label for="mobile">mobile</label>
                        <input type="text" class="form-control" id="job_title" value="{{ auth()->user()->job_title }}" name="job_title">
                    </div>
                </div>
            </div>
		</div>
	</div>
	<b>social network</b>
    <div class="mb-2 bg-white p-3 mb-5">
        <div class="row p-3" id="showSocial">

        </div>
        <div class="w-100 text-center">
            <p>add a social network</p>
            <a id="addSocial" class="bg-primary text-white pt-2 pb-2 pl-3 pr-3 rounded-circle cursor-pointer">
                <i class="fas fa-plus"></i>
            </a>
        </div>
    </div>
    <div class="form-group mb-5">
        <button type="submit" class="btn btn-success">save</button>
    </div>
</form>

<script>
	$(document).ready(function() {
		let countSocial = 1;
		$('#addSocial').click(function () {
			countSocial++;
			dynamicSocial(countSocial);
		});
		dynamicSocial(countSocial);
		function dynamicSocial (number) {
			let html = '' +
				'<div class="col-md-6 position-relative">\n' +
				'<i class="fas fa-times text-danger position-absolute"></i>' +
				'<div class="row">\n' +
				'<div class="col-md-4">\n' +
				'<div class="form-group">\n' +
				'<label for="social_id">Social Network</label>\n' +
				'<select id="social_id" name="social_id[]" class="form-control">\n' +
				'@foreach($socials as $social)\n'+
				'<option value="{{ $social->id }}">\n' +
				'{{ $social->name }}\n' +
				'</option>\n' +
				'@endforeach\n ' +
				'</select>\n' +
				'</div>\n' +
				'</div>\n' +
				'<div class="col-md-8">\n' +
				'<div class="form-group">\n' +
				'<label for="related_id">Related ID</label>\n' +
				'<input id="related_id" name="related_id[]" class="form-control">\n' +
				'</div>\n' +
				'</div>\n' +
				'</div>\n' +
				'</div>';
			$('#showSocial').append(html);
		}
	});
</script>
shahr's avatar
Level 10

I updated my post. Please answer my question. Thanks

MichalOravec's avatar
Level 75

related_id and social_id are array.

In pivot table between socials and users, social_id is foreign key from socials table and not from social_networks table.

related_id you have in social_networks table.

So don't mix it together.

$user->socials()->sync($request->social_id);

foreach ($request->related_id as $related)
    $user->socialNetworks()->create(['related_id' => $related]);
}
1 like
Snapey's avatar

I don't know why you have pivot table social_user Your social network only belongs to one user - it won't have the related_id to belong to another user. The relationship should be 1:many not many:many ?

1 like
shahr's avatar
Level 10

@michaloravec

I get this error

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into social_networks (related_id, updated_at, created_at) values (oxbir, 2020-10-04 09:44:40, 2020-10-04 09:44:40))

Please or to participate in this conversation.