Ad

I Can't Get Data When I Am Editing In Laravel 5.1

i have address Book table and user table i am assigning 

the many user in my address book while i am created everything is fine(ok) but when i am editing every data back in my form without assign user . how can i get the user in editing form ?? this is my Address Book Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\AddressRequest;
use App\Http\Requests;
use App\Models\Address;
use App\Models\User;
use App\Http\Controllers\Controller;`enter code here`
use Illuminate\Pagination\Paginator;
use Auth;
use DB;
use Session;

class AddressesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        Session::forget('searchaddress');

       $addresses = Address::orderby('company_name');

        $company_name = $request->input('company_name');

        if(!empty($company_name)) {

              //$addresses->where('company_name','LIKE','%'.$company_name.'%');

             $addresses->where('company_name','LIKE','%'.$company_name.'%');
            Session::set('searchaddress', $company_name);
        }
        $addresses = $addresses->paginate(5);


        return view('address.index',compact('addresses'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {

        $users = User::lists('first_name','id');

        return view('address.create',compact('users'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(AddressRequest $request)
    {

        $address = Address::create($request->all());
        $firstname = Auth::user()->first_name;
        $lastname = Auth::user()->last_name;
        $address->created_by =$firstname." ".$lastname;

        $address->users()->attach($request->input('user_list'));
        $address->save();
        return redirect('/addresses');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $address = Address::find($id);
        return view('address.show',compact('address'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {   $users = User::lists('first_name','id');
        $address = Address::findorFail($id);

        return view('address.edit',compact('address','users'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update( AddressRequest $request ,$id)
    {

        $address = Address::findOrFail($id);
        $address->update($request->all());
        $address->users()->sync($request->input('user_list'));
        return redirect('/addresses');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $address = Address::find($id);
        $address->delete();
        return redirect('/addresses');
    }


}
 and that is my AddressBook Model 
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    protected $fillable = [
    'created_by',
    'company_name',
     'phone', 
     'email', 'address','comment'
    ];
    public function users()
    {
        return $this->belongsToMany('App\Models\User')->withTimestamps();
    }
    public function getUserListAttribute()
    {
        return $this->users->lists('id');
    }
}

 [![enter image description here][1]][1]


  [1]: http://i.stack.imgur.com/D4jXQ.png
Ad

Answer

You have belongsToMany relation, so in your edit action you also should get current Address users like this

public function edit($id)
    {   $users = User::lists('first_name','id');
        $address = Address::findorFail($id);
        $address_users = $address->users->lists('id')->toArray();

        return view('address.edit',compact('address','users', 'address_users'));
    }

then in your view in select you should intersect arrays of $users and $address_users to get selected options.

{!! Form::select('user_list[], $users, isset($address_users) ? $address_users : null, ['id' => 'users_list', 'class' => 'form-control', 'multiple']) !!}

to avoid

isset($address_users) ? $address_users : null

you can define empty address_users array in your create method and do it like this

$address_users
Ad
source: stackoverflow.com
Ad