Francisunoxx Francisunoxx - 4 months ago 60
PHP Question

Sync method won't work using many to many relationship Laravel 5.2

I'm trying to attach the array of items that I have in my form to my junction table. I already given all the values stored in my tables using

foreach
loop. I loop this all in my select list.

Retrieve all records in tables:

$resultRecipient = DB::table('users')->where('id', '!=', Auth::id())->get();
return view ('document.create')->with('resultRecipient', $resultRecipient);


Form:

<div class = "form-group">

<label for = "recipient_id" class = "control-label">To:</label>

<select name = "recipient_id[]" multiple class = "form-control" id = "myUserList">

@foreach ($resultRecipient as $list)
<option value = "{{ $list->id }}">{{ $list->username }}</option>
@endforeach

</select>

</div>


Migration:

users

-id

-username

-password

documents

-id

-title

-content

-category_id - FK

recipients_documents (junction table)

-id

-senderUserId - FK reference to users

-recipientUserId - FK reference to users

-docu_id - FK reference to documents

My Models:

Document


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Document extends Model
{

public function recipients()
{
return $this->belongsToMany('Models\User');
}
}


User:

<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class User extends Model implements AuthenticatableContract
{
use Authenticatable;

public function documents()
{
return $this->belongsToMany('Models\Document');
}
}


DocumentController:

public function postDocuments(Request $request)
{

$this->validate($request,
[
'title' => 'required|alpha_dash|max:255',
'content' => 'required',
'category_id' => 'required',
'recipient_id' => 'required',
]);


$document = new Document();
//Request in the form
$document->title = $request->title;
$document->content = $request->content;
$document->category_id = $request->category_id;

$document->save();
$document->recipients()->sync($request->recipient_id, false);


return redirect()->back();
}


When I'm trying to attach the request in my form which is
recipient_id
but it error says. I just created a new folder in app named Models


Class 'Models\User' not found


Can anyone tell why I get this error? Any help would appreciated!

Cheers

UPDATE

I solved this error followed steps by @jaysingkar. But when I try to
sync
this after hitting the submit button it gives me a error.


SQLSTATE[42S02]: Base table or view not found: 1146 Table 'webdev.document_user' doesn't exist (SQL: select
user_id
from
document_user
where
document_id
= 15)


But it insert in the document tables but in my juction table won't insert that I want to attach.

Answer

Along with @Marcin 's answer,
You would need to use "App\Models\User" instead of "Models\User"

public function recipients()
{
    return $this->belongsToMany('App\Models\User');
}