我正在尝试编辑记录,但我希望看到记录的名称,如果我使用agency_id我的表单中获取ID,但我想使用agency->名称并获取该名称记录,我如何在我的控制器中执行此操作?
我的控制器
public function index()
{
$accounts = Account::all();
return view('accounts',compact('accounts'));
}
public function edit(Request $request, $id)
{
$account=Account::find($id)->ith;
$request->session()->flash('alert-success', 'Account has been updated');
return view('accounts.edit',compact('account'));
}
public function update(Request $request, $id)
{
$account = Account::find($id);
$account->update($request->all());
$request->session()->flash('alert-success', 'Account successfully updated');
return redirect('accounts');
}
public function show(Request $request, $id)
{
$accounts= $request->all();
return view('accounts.show',compact('accounts'));
}
public function store(Request $request)
{
$account = new Account;
$type = new Type;
$agency = new Agency;
$client = new Client;
$agency->name = $request->name; $agency->save();
$type->name= $request->name; $type->save();
$client->name= $request->name; $client->save();
$account->url= $request->url; $account->save();
$account->client()->associate($client);
$account->type()->associate($type);
$account->agency()->associate($agency);
$request->session()->flash('alert-success', 'New account successfully created');
return redirect('accounts');
}
我的表格
<div class="form-group col-md-6">
<div class="form-group">
{!! Form::label('Name', 'Name:') !!}
{!! Form::text('client->name',null,['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group col-md-6">
<div class="form-group">
{!! Form::label('Agency', 'Agency:') !!}
{!! Form::text('agency->name',null,['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group col-md-6">
<div class="form-group">
{!! Form::label('URL', 'Url:') !!}
{!! Form::text('url',null,['class'=>'form-control']) !!}
</div>
</div>
答案 0 :(得分:2)
首先,您必须有一个方法来编辑帐户:
public function edit($id)
{
$account = Account::find($id);
return view('account.edit', ['account' => $account]);
}
这将允许您填写表格,例如:
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', $account->name, ['class'=>'form-control']) !!}
</div>
当您提交此表单时,您将更新该帐户。
public function update(Request $request, $id)
{
$account = Account::find($id);
$account->fill($request->all())->save();
return redirect('accounts')->with('alert-success', 'Account successfully updated');
}
答案 1 :(得分:0)
试试这样:
public function edit(Request $request, $id)
{
$account = Account::where('id' , $id)->first();
// you can try also
// $account=Account::where('id' ,'=', $id)->first();
$request->session()->flash('alert-success', 'Account has been updated');
// make a test, so you can see if it work and return name of agency! like this:
return $account->name;
// if it return the name of agency it work! and you can try to return in your form!
return view('accounts.edit',compact('account'));
}
你可以这样使用:
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', $account->name, ['class'=>'form-control']) !!}
</div>