使用我的表单创建项目后,我可以看到已创建项目的列表(标题(字符串),代码(字符串),域(下拉列表),所有者(下拉列表)。 我只能看到 id,而不是显示之前在下拉列表中选择的域或所有者的名称。我不知道如何解决这个问题。
这是我的迁移create_projects_table:
public function up()
{
Schema::create('pros', function (Blueprint $table) {
$table->increments('id', true);
$table->string('title');
$table->string('code');
$table->integer('domain_id')->unsigned();
$table->foreign('domain_id')->references('id')->on('domains');
$table->integer('owner_id')->unsigned();
$table->foreign('owner_id')->references('id')->on('owners');
table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('projects');
}
项目控制器 :
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()
{
$domains = Domaine::all('nameDomain', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projects.create', compact('domains', 'owners'));
}
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'owner_id' => 'required',
'domain_id' => 'required'
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success','Project created successfully');
}
public function show($id)
{
$domains = Domain::all('nameDomain', 'id');
$owners = Owner::all('nameOwner', 'id');
$project = Project::find($id);
return view('projects.show',compact('project', 'domains', 'owners'));
}
显示刀片.php :
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Quartier:</strong>
{{ $pro->quartier_id }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Responsable:</strong>
{{ $pro->responsable_id }}
</div>
</div>
索引刀片.php :
<!-- ........ -->
<table class="table table-bordered">
<tr>
<th>No</th>
<th>title</th>
<th>code</th>
<th>domain_id</th>
<th>owner_id</th>
<th width="280px">Action</th>
</tr>
@foreach ($projects as $key => $project)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $project->title }}</td>
<td>{{ $project->code }}</td>
<td>{{ $project->domain_id}}</td>
<td>{{ $project->owner_id}}</td>
<!-- ...... -->
项目.php :
class Pro extends Model
{
//
public $fillable = ['title','code','domain_id', 'owner_id'];
}
这是我添加 dd($projects) 后得到的; :
LengthAwarePaginator {#285 ▼
#total: 3
#lastPage: 1
#items: Collection {#327 ▼
#items: array:3 [▶]
}
#perPage: 5
#currentPage: 1
#path: "http://127.0.0.1/Projet_PAC/Projet_PAC/public/pros"
#query: []
#fragment: null
#pageName: "page"
}
然后在添加 {{ dd($project->域) }} 作为每个循环的第一行之后
Domain {#339 ▼
#table: "domains"
+fillable: array:1 [▼
0 => "domain"
]
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:4 [▼
"id" => 2
"nameDomain" => "domaine2"
"created_at" => "2017-02-01 22:55:25"
"updated_at" => "2017-02-01 22:55:25"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
+exists: true
+wasRecentlyCreated: false
}
您需要为模型设置适当的雄辩关系,例如:
namespace AppProject;
Class Project {
public function domain() {
return $this->belongsTo('AppDomaine');
}
public function owner() {
return $this->belongsTo('AppOwner');
}
}
现在,在视图中,您可以使用:
@foreach ($projects as $key => $project)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $project->title }}</td>
<td>{{ $project->code }}</td>
<td>{{ $project->domain->name }}</td>
<td>{{ $project->owner->name }}</td>
</tr>
@endforeach
注意:我假设您的所有者和域表中有"名称"列
请看一下这个:https://laravel.com/docs/master/eloquent-relationships#one-to-many
您与其他模型没有关系。试试这个:
您的 Pro.php 模型应具有以下功能:
public function domaine() {
return $this->hasOne(Domaine::class);
}
public function owner() {
return $this->hasOne(Owner::Class);
}
在视图中使用以下命令:
@foreach ($projects as $project)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $project->title }}</td>
<td>{{ $project->code }}</td>
<td>{{ $project->domaine->id }}</td>
<td>{{ $project->owner->id }}</td>
</tr>
@endforeach
(删除我之前要求您添加的 dd($projects)