我有一个需要显示金额的页面(admin.blade.php)。页面中有一个按钮('更改'),它调用一个输入的模态。此模式存在于表单内部,其操作称为“更改目标”路径,后者又从控制器获取changeGoal函数。
我的admin.blade.php包含表单,模式,如下:
<a class="btn btn-primary" data-toggle="modal" data-target="#moneyModal">
<span class="glyphicon glyphicon-money glyphicon-white"></span>change</a>
<div class="modal fade" id="moneyModal" role="dialog">
<div class="modal-dialog">
<form method="post" action="change-goal">
{{csrf_field()}}
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Change Goal</h4>
</div>
<div class="modal-body">
<p>Please enter a new goal.</p>
</div>
<div class="modal-body">
<input class="form-control" name="newGoal" id="newGoal">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="save" type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</form>
</div>
</div>
我的控制器如下:
public function changeGoal(Request $data) {
$newGoal =DB::table('user')->whereColumn('goal')->get();
$newGoal->updateGoal($data->goal);
return view('admin',compact('newGoal'));
}
我的用户模型是:
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'goal'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function updateGoal($goal){ $this->goal = $goal; $this->save(); }
我想在admin.blade.php页面上显示newGoal的值。目前我编写了{{$ newGoal}},但它给出了一个错误,说明未定义的变量:newGoal。
我猜这个问题在控制器内。
我如何能够保存在该模态/表单中输入的值 我每次访问该页面时都能看到它。
此外,这是表格,如果需要:
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('password');
$table->integer('goal')->default(10000);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
真的很感激帮助。
编辑: 我的adminmanagement.blade.php(以前称为admin.blade.php)是:
<p>Total Donations: <br> ${{ $price }}</p>
<p>Our Goal: {{ $newGoal }}</p>
<a class="btn btn-primary" data-toggle="modal" data-target="#moneyModal">
<span class="glyphicon glyphicon-money glyphicon-white"></span>change</a>
<div class="modal fade" id="moneyModal" role="dialog">
<div class="modal-dialog">
<form method="post" action="change-goal">
{{csrf_field()}}
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Change Goal</h4>
</div>
<div class="modal-body">
<p>Please enter a new goal.</p>
</div>
<div class="modal-body">
<input class="form-control" name="newGoal" id="newGoal">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="save" type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</form>
</div>
</div>
web.php有:
Route::group(['middleware' => 'auth'], function () {
Route::get('/home', 'PagesController@getAllVideos')->name('/home');
Route::post('change-goal','PagesController@changeGoal')->name('change-goal');
控制器:
public function getAllVideos(){
$videos = Video::all();
$price = DB::table('donor')->sum('amount_donated');
return view('adminmanagement',compact(['videos','price']));
}
public function changeGoal(Request $data){
$newGoal = $data->input('newGoal');
auth()->user()->update([
'goal' => $newGoal
]);
return view('adminmanagement', compact('newGoal'));
}
用户模型:
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'goal'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function updateGoal($goal){
$this->goal = $goal;
$this->save();
return $this;
}
}
答案 0 :(得分:1)
根据您的评论和代码,您的输入字段为newGoal
而非goal
。您的用户提取查询错误,whereColumn
条件缺少该值。你使一个简单的操作变得复杂。
根据您的意见,您需要做的就是更新登录用户(admin)的目标并返回新的目标值。这样做
public function changeGoal(Request $request)
{
$newGoal = $request->input('newGoal');
auth()->user()->update([
'goal' => $newGoal
]);
return view('admin', compact('newGoal'));
}
修改:根据您的评论。你应该这样做以保持简单。
public function getAllVideos()
{
$videos = Video::all();
$price = DB::table('donor')->sum('amount_donated');
$goal = auth()->user()->goal;
return view('adminmanagement', compact('videos', 'price', 'goal'));
}
public function changeGoal(Request $data)
{
auth()->user()->update([
'goal' => $data->input('newGoal')
]);
return redirect('/home');
}
在您的视图中将$newGoal
更改为$goal
。
<p>Total Donations: <br> ${{ $price }}</p>
<p>Our Goal: {{ $goal }}</p>
答案 1 :(得分:0)
内部模型
public function updateGoal($goal){
$this->goal = $goal;
$this->save();
return $this;
}
并在您的控制器内
public function changeGoal(Request $data) {
$oldGoal =DB::table('user')->whereColumn('goal')->get();
$newGoal = $oldGoal->updateGoal($data->goal);
return view('admin',compact('newGoal'));
}