Many to Many Relatioship within the same table

Posted in CategoryLaravel
  • G
    George 2 months ago

    I have three tables, users, file_user and files . Within the user I have one user who acts as the admin. This user receives creates files and can share with the other users. I have a form for creating the files where he can add the users on a signle file. 

    The problem is when I save the the file. Only this user who is the admin has his id saved in the file_user pivot table.

     

    This is my code summary

    <?php

    public function create( )
    {
    if (Auth::user()->isAdmin()){

    $view = view('backend.auth.file.create');
    $view->doctypes = Doctype::all()->pluck('name','id')
    $view->users = User::all()->pluck('first_name','id')
    $view->folders = Folder::pluck('name', 'id');

    return $view;
    }else{

    $view = view('backend.auth.file.create');
    $view->doctypes = Doctype::where('org_id', Auth::id())->pluck('name','id');
    $view->users = User::where('org_id', Auth::id())->pluck('first_name','id');
    $view->folders = Folder::pluck('name', 'id')
    return $view;
    }
    }
    //This is the saving function for the file in the FileController.php
    public function store(StoreFileRequest $request)
    {

                $dataToBeSaved = $request->all();
                $dataToBeSaved['description'] = strip_unsafe_tags($dataToBeSaved['description']);
                $file = $request->file('path');
                $destinationPath = 'uploads/files';
                $dataToBeSaved['path'] = $request->file('path')->store($destinationPath, 'public');
                $file = File::create($dataToBeSaved);
                $users = array_where($request->get('users'), function ($value) {
                    return ($value);
                });
                $file->users()->sync($users);
                return redirect()->route('admin.auth.file.index');
    }
        
    // This is the File.php Model

    protected $table = 'files';
    protected $fillable = ['name','description','path','org_id','doctype_id','folder_id',];
    public function users()
    {
    return $this->belongsToMany(User::class, 'file_user', 'file_id', 'user_id');
    }
    // This is the User.php Model
        /**
    * @return mixed
    */
    public function files(){
    return $this->belongsToMany(File::class, 'file_id');
    }

    // Part of the form for picking the id
    <div class="col-md-10">
    @if(!empty(old('users')))
    <?php $data = old('users'); ?>
    <?php session()->forget('_old_input.users'); ?>
    @elseif(!empty($file->users))
    <?php $data = $file->users->pluck('id'); ?>
    @endif

    @if(empty($data) || !count($data))
    <?php $data[0] = null; ?>
    @endif

    @foreach($data as $item)
    <div class="input-group control-group after-add-more">
    {!! Form::select('users[]', $users, $item, ['class' => 'form-control'])!!}
    <div class="input-group-btn">
    <button class="btn btn-success add-more" type="button"><i class="glyphicon glyphicon-plus"></i> Add</button>
    </div>
    </div>

    @endforeach
    </div>
    <!-- Copy Fields -->
    <div class="copy hide">
    <div class="control-group input-group" style="margin-top:10px">
    {!! Form::select('users[]', $users, $item, ['class' => 'form-control'])!!}
    <div class="input-group-btn">
    <button class="btn btn-danger remove" type="button"><i class="glyphicon glyphicon-trash"></i> Remove</button>
    </div>
    </div>
    </div>

    Can someone show me why am not able to store the selected users id

Please login or register to leave a response.

* You May Also Like Bellow Issue to check *