【问题标题】:How to use foreign keys to retrieve data || laravel 5.3如何使用外键检索数据 ||拉拉维尔 5.3
【发布时间】:2017-06-16 19:06:02
【问题描述】:

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

这是我的项目控制器:

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>

模型项目.php:

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

迁移:创建项目表:

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');
}

知道为什么列表总是空的吗?我有播种机,所以数据库中的表不为空

【问题讨论】:

    标签: mysql laravel foreign-keys


    【解决方案1】:
    <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>
    

    在这部分代码中,您检查了 empty($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() 而不是 pluck。

    【讨论】:

    • 非常感谢,这正在工作。好吧,它显示数据的方式并不是我真正想要的(类似这样的: {"domaine":"name_domaine1","id":1} )。但是,至少列表不再是空的。
    • 欢迎,希望对您有所帮助。
    【解决方案2】:

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

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

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

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

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

    【讨论】:

    • 我尝试了 all() 但是,它并没有改变任何东西。并且 get() 生成此错误:传递给 Illuminate\Database\Grammar::columnize() 的参数 1 必须是数组类型,给定字符串,在 C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds 中调用-www\PC\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php 在第 123 行并定义
    • @Naj 你有没有尝试迭代pluck() 结果,正如我所展示的那样?另外,在使用get()时尝试使用这个:get(['name', 'id'])
    • 是的,我迭代了 pluck() 结果,并将 pluck() 替换为 get()
    • @Naj 同时?您应该将pluck() 与我提供的迭代代码一起使用,或者您应该使用get() 并像以前一样迭代数据。如果您做对了,但在使用pluck() 时仍然出现任何错误,请在此处发布。
    • 不,不是同时。这些方法都不起作用:(
    猜你喜欢
    • 2017-02-03
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2016-10-20
    • 2018-02-25
    相关资源
    最近更新 更多