如何使用外键来检索数据|| laravel 5.3

时间:2017-01-31 11:06:24

标签: mysql laravel foreign-keys

我正在使用laravel 5.3和MySQL。我试图使用外键从不同的表中检索数据。将显示我从其他表“domaines”获得的数据的字段是一个下拉列表,但它始终为空。

这是我的ProjectController:

class ProjectController extends Controller
{
    public function index(Request $request)
    {
         $projects = Project::orderBy('id','DESC')->paginate(5);

         return view('projects.index',compact('projects'))
            ->with('i', ($request->input('page', 1) - 1) * 5);
    }

    public function create()
    {
        $domaines = Domaine::pluck('name', 'id');

        return view('projects.create', compact('domaines'));
    }

    public function store(Request $request)
    {
        $domaine_id = Domaine::all()->pluck('name', 'id');     

        $this->validate($request, [
            'title' => 'required',
            'code' => 'required',       
            'domaine_id' => 'required'
            ]);

        Project::create($request->all());

        return redirect()->route('projects.index') //???? add compact or not ??
                    ->with('success','Project created successfully');
    }

    public function show($id)
    {
        $project = Project::find($id);

        return view('projects.show',compact('project'));
    }

    public function edit($id)
    {
        $project = Project::find($id);
        return view('projects.edit',compact('project'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required',
            'code' => 'required',
            'domaine_id' => 'required'
        ]);

        Project::find($id)->update($request->all());

        return redirect()->route('projects.index')
                    ->with('success','Project updated successfully');
    }

    public function destroy($id)
    {
        Project::find($id)->delete();

        return redirect()->route('projects.index')
                    ->with('success','Project deleted successfully');
    }
}

这是show.blade.php:

<div class="col-xs-12 col-sm-12 col-md-12">
    <div class="form-group">
        <strong>Domaine:</strong>
        {{ $project->domaine_id }}
    </div>
</div>

create.blade.php:

<select class="form-control" name="domaine_id">
            @if (empty($domaine_id))                                    
                Whoops! Something went wrong                            
            @else
            @foreach($domaines as $domaine)
              <option value="{{$domaine->id}}">{{$domaine->name}}</option>
            @endforeach
            @endif
</select>

模型Project.php:

<?php
 namespace App;
 use Illuminate\Database\Eloquent\Model;
 use App\Domaine;
 class Pro extends Model
 {
     public $fillable = ['title','code','domaine_id'];
 }

迁移:create_projects_table:

public function up()
{
    Schema::create('projects', function (Blueprint $table) {
        $table->increments('id', true);
        $table->string('title');
        $table->string('code'); 
        $table->integer('domaine_id')->nullable();
        $table->foreign('domaine_id')->references('id')->on('domaines');        
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('projects');
}

知道为什么列表总是空的?我有播种机,所以DB中的表格不是空的

2 个答案:

答案 0 :(得分:1)

  

知道为什么列表总是空的?

由于您使用的是pluck()方法,因此您应该像这样迭代列表:

@foreach($domaines as $id => $domaine)
     <option value="{{ $id }}">{{ $domaine }}</option>
@endforeach

或者只使用get()all()而不使用pluck()

$domaines = Domaine::get(['name', 'id']);

答案 1 :(得分:0)

<select class="form-control" name="domaine_id">
            @if (empty($domaine_id))                                    
                Whoops! Something went wrong                            
            @else
            @foreach($domaines as $domaine)
              <option value="{{$domaine->id}}">{{$domaine->name}}</option>
            @endforeach
            @endif
</select>

在这段代码中你检查为空($ domaine_id),但是从你的控制器中,你没有在视图加载期间传递这个变量。

public function create()
{
   $domaines = Domaine::pluck('name', 'id');
   return view('projects.create', compact('domaines'));
}

您可以更改if条件,如下所示:

<select class="form-control" name="domaine_id">
            @if (!count($domaines) > 0)                                    
                Whoops! Something went wrong                            
            @else
            @foreach($domaines as $domaine)
              <option value="{{$domaine->id}}">{{$domaine->name}}</option>
            @endforeach
            @endif
</select>

顺便使用all()而不是使用控制器。