Laravel 5.2属于很多观点

时间:2016-07-16 20:35:31

标签: php laravel eloquent many-to-many laravel-5.2

我正在尝试在人和地点之间在Laravel 5.2.39中建立一个属于多人的关系但是无法将信息传递到选择框中的视图中。我得到:“错误异常:未定义的变量:人”。它也没有保存并制作数据透视表条目。

我创建了数据透视表person_place,其中'place_id'和'person_id'列对应于'people'和'places'表。

App / Person.php (人物模型)

<?php
namespace ss;
use ss\Place;

use Illuminate\Database\Eloquent\Model;

class Person extends Model
{
    /**
     * Fillable fields
     * 
     * @var array
     */
    protected $fillable = [
        'name',
        'type',
        'status',
        'notes',
        'email',
        'phone',
        'alt',
        'web',
        'first',
        'last',
        'title',
    ];

    public function places()
    {
        return $this->belongsToMany('Place');
    }
}

App / Place.php (放置模型)

<?php
namespace ss;
use ss\Person;

use Illuminate\Database\Eloquent\Model;

class Place extends Model
{
    /**
     * Fillable fields
     * 
     * @var array
     */
    protected $fillable = [
        'name',
        'type',
        'status',
        'notes',
        'email',
        'phone',
        'alt',
        'web',
        'address1',
        'address2',
        'city',
        'state',
        'zip',
        'country',
    ];

    public function people()
    {
        return $this->belongsToMany('Person');
    }

}

App / Http / Controllers / PlaceController.php (位置控制器)

 <?php namespace ss\Http\Controllers;
use ss\Place;
use ss\Person;
use ss\Http\Requests;
use Illuminate\Http\Request;
use View;
use Session;

class PlaceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $places = Place::all();
        $people = Person::pluck('name','id')->all();

        return View::make('places.index', compact('places','people'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $people = Person::lists('last', 'id');

        return view('places.create', compact('people'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required'
        ]);

        $input = $request->all();
        Place::create($input);

        Session::flash('flash_message', 'Place successfully added!');
        return redirect()->action('PlaceController@index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $place = Place::findOrFail($id);
        return view('places.show')->withPlace($place);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $place = Place::findOrFail($id);
        $people = Person::pluck('name','id')->all();

        return view('places.edit', compact('place','people'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id, Request $request)
    {
        $this->validate($request, [
            'name' => 'required'
        ]);

        $input = $request->except('people');

        $place = Place::findOrFail($id);
            $peopleIDs = $this->place->people($peopleIds);

        $place->fill($input)->save();
        Session::flash('flash_message', 'Place successfully edited!');
        return redirect()->action('PlaceController@index', $place);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $place = Place::findOrFail($id);
        $place->delete();

        Session::flash('flash_message', 'Place successfully deleted!');
        return redirect()->action('PlaceController@index');
    }

}

resources / views / places / edit.blade.index.php (地方视图,我正在尝试构建选择列表 - 不工作)

@section('aside')
        {!! Form::select('people', $people, null, ['class' => '', 'multiple']) !!}

<ul class="section table-of-contents">
    <li><a href="#introduction">New Time</a></li>
    <li><a href="#structure">New Money</a></li>
    <li> </li>
    <li><a href="#initialization">New Sub-Task</a></li>
</ul>
@endsection

resources / views / places / create.blade.index.php (使用选择列表放置视图,但保存时无法生成数据透视表)

@section('aside')
            {!! Form::select('people', $people, null, ['class' => '', 'multiple']) !!}
@endsection

尝试过“with”和“attach”的各种方法,但无法得到结果。有趣的是,即使我专门放置“$ person =”foo“;在我的编辑方法中,它仍然表示未定义。

感谢您的帮助。

编辑:更新代码

1 个答案:

答案 0 :(得分:0)

在PlacesController中,您的edit($id)方法不会将一组人员传递给视图。

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    $place = Place::findOrFail($id);
    $people = People::pluck('name','id')->all();
    return view('places.edit', compact('place', 'people');
}