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

时间:2018-03-16 20:02:16

标签: mysql forms laravel

路线:

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

我的控制员:

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张图片(复制品)需要两张。如何在数据库中保存选中的复选框以及如何以show form显示两个输入/图像?谢谢你

1 个答案:

答案 0 :(得分:0)

尝试attach保存关系:

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