Laravel Eloquent / Many to Many Relations gives error ( Array to string conversion )

Published

I want to display all job posts that user have applied

Here is my Tables ..

applies   ->  | users_id | posts_id |
posts     ->  | id       | (other posts cols ... )
user_info ->  | id       | (name col etc...)

Ive tried belongsToMany() but gives error

Array to string conversion

Post model Relation

public function applies()
{
   return $this->belongsToMany(Applies::class , 'users_id' , 'posts_id');
}

Applies model

   protected $table = 'applies';
    protected $primaryKey = ['user_id', 'id'];
    public $incrementing = false;
    protected $fillable = [
        'user_id',
        'posts_id'
    ];

Lastly Controller

public function index()
{
    $infos = Info::where('user_id', Auth::id())->first();
    $apply = Post::find(2)->applies ;
    var_dump($apply);
}

Source: Laravel

Published
Categorised as eloquent, laravel, php Tagged , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq