@oxbir Show your form's render HTML please.
Oct 3, 2020
7
Level 10
How to save social network?
My form is like this
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
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
Please or to participate in this conversation.
