I have a pivot model for a Many-to-Many relationship called UserWebpage.
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class UserWebpage extends Pivot
{
public function collections(){
return $this->belongsToMany('App\Collection', 'collection_user_webpage');
}
}
I also have a model called Collection
namespace App;
use Illuminate\Database\Eloquent\Model;
class Collection extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function userWebpages(){
return $this->belongsToMany('App\UserWebpage', 'collection_user_webpage');
}
}
I'm trying to establish a Many-to-Many relationship between UserWebpage and Collection. I know something like this should be possible based on this SO question that I found: Laravel: How to use multiple pivot table relationships
My issue is this:
When I try to insert a relationship between a UserWebpage instance and a Collection instance, I get the following error.
Code to insert the relationship:
App\UserWebpage::where('user_id', $user->id)->take(2)->get()->each(function($user_webpage) use ($user){
$collection = factory(App\Collection::class)->create(['user_id' => $user_webpage->user_id]);
$collection->userWebpages()->attach($user_webpage->id);
});
The error I get:
Illuminate\Database\QueryException : SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'field list' (SQL: insert into
collection_user_webpage
(``,collection_id
) values (1, 1))
Is it possible to create this type of a relationship? Or am I missing something?