gpt4 book ai didi

mysql - 如何在 Laravel 中用图像在输入复选框中显示来自 mysql 的数据?

转载 作者:行者123 更新时间:2023-11-30 21:46:15 24 4
gpt4 key购买 nike

路线:

Route::get('/slider', 'UserController@showTemplete')->name('slider');
Route::post('/slider-choose','UserController@saveTemplete')->name('choose');

我的 Templete 模型:

class Templete extends Model
{
protected $table='templete';
public function Users()
{
return $this->belongsToMany(Users::class, 'users_templetes', 'user_id', 'templete_id');
}
}

我的播种机:

public function run()
{
$names=['temp1', 'temp2'];
foreach($names as $name){
$templete = new Templete();
$templete->name=$name;
$templete->save();
}
}
}

我的 Controller :

public function showTemplete(){
$templetes= Templete::all();
return view('defaultLayout.RegisterPageContentChoose', ['templetes'=>$templetes]);
}

public function saveTemplete(Request $request){
$templeteId = $request->get('templete');
$user=Users::find(Auth::id());
$user->templete=$templete;
$user->save();
return view('slider.slider');
}

但是数据没有保存在数据库中!!!这是我的表格:

<form method="post" action="{{route('choose')}}">
<div class="form-group">
@foreach($templetes as $templete)
<div class="col-md-5 pull-right"><label class="btn">
<img src="/images/free.png" class=" img-responsive img-thumbnail img-check" style= "height: 500px; width: 100%"><input type="checkbox" name="templetes" value="{{$templete->id}}" class="hidden" autocomplete="off"></label>
</div>
<div class="col-md-5 col-md-5-offset-2 pull-left"><label class="btn">
<img src="/images/moecv2.png" class=" img-responsive img-thumbnail img-check" style= "height: 500px; width: 100%" ><input type="checkbox" name="templetes" value="{{$templete->id}}" class="hidden" autocomplete="off"></label>
</div>
@endforeach
</div>
<button>@lang('login.account_choose_button')</button><br><br>
<!-- <input type="submit" value="Check Item" class="btn btn-success"> -->
</form>

显示 4 张图片(重复)需要两张。如何将选中的复选框保存在数据库中以及如何以显示形式显示两个输入/图像?谢谢你

最佳答案

尝试 attach保存关系:

public function saveTemplete(Request $request){         
$templeteId = $request->get('templete');
$user=Users::find(Auth::id());
$user->templete()->attach($templete_id);
return view('slider.slider');
}

关于mysql - 如何在 Laravel 中用图像在输入复选框中显示来自 mysql 的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49328676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com