在输入中爆炸并插入 bdd

explode in input & insert bdd

我在插入表单时遇到问题。就是恢复代码,在Laravel 5.3下,我只是隐约知道。

因此我必须自己适应代码,实际上我的数据库中有一个字段描述,它分为两个: "champ1" 和 "champ2" 在显示中一切都很好,但是当我想插入时,它不起作用..有人可以指导我找到解决方案或一首曲目

我的看法:

<form method="POST" action="{{ url('/update/'.$data->id) }}" enctype="multipart/form-data">
                    {{ csrf_field() }}
                    <img style="height:100px;" src="{{ URL::asset("storage/".$data["categorie"]->libelle."/micro/".$data->url)}}" alt="" />
                    <label for="upload">Image de l'article :</label>
                    <input type="file" name="upload" />

                    @if (count($data->langues) > 0)

                        @foreach ($data->langues as $langue)
                        <h3>{{ $langue->libelle }}</h3>
                        <input type="text" name="titrel{{ $langue->id }}" placeholder="Titre en {{ $langue->libelle }}" 
                        value="{{old("titrel".$langue->id,$data->texts[$langue->id]->titre)}}" />
                        <label for="descriptionl{{ $langue->id }}">Description en {{ $langue->libelle }}</label>
                        <textarea type="text" name="descriptionl{{ $langue->id }}" 
                        placeholder="Description en {{ $langue->libelle }}">{{old("descriptionl".$langue->id,$data->texts[$langue->id]->description)}}</textarea>
                        <textarea type="text" name="champ1l{{ $langue->id }}" 
                        placeholder="Aperçu en {{ $langue->libelle }}">{{old("champ1l".$langue->id,$data->texts[$langue->id]->champ1)}}</textarea>
                        <textarea type="text" name="champ2l{{ $langue->id }}" 
                        placeholder="Description en {{ $langue->libelle }}">{{old("champ2l".$langue->id,$data->texts[$langue->id]->champ2)}}</textarea>
                        @endforeach

                @endif
                    <input type="submit" value="Mettre à jour" />
                </form>

我的控制器:

public function edit($id){
    $article = Article::find($id);
    if($article == null)
        return redirect("/home");
    $article->langues=Langue::all();
    $test= array();
    foreach($article->langues as $langue){
        $test[$langue->id]=TextArticle::firstOrNew(['idlangue' => $langue->id,'idarticle'=>$id]);
        $myString=$test[$langue->id]->description;
        $myArray = explode(';', $myString);
        if(count($myArray)>1){
            $test[$langue->id]->champ1=$myArray[0];
            $test[$langue->id]->champ2=$myArray[1];
        }
        elseif (count($myArray)==1) {
            $test[$langue->id]->champ1="";
            $test[$langue->id]->champ2=$myArray[0];
        }
    }
    $article->texts=$test;
    $article->categorie=Categorie::find($article->idcategorie);
    //var_dump($test);
    return view('articles/edit')->with("data",$article);


}

public function update(Request $request, $id){
    $article = Article::find($id);
    $categorie = Categorie::find($article->idcategorie);
    if($article == null)
        return redirect("/home");
    $validation=[];
    $langues=Langue::all();
    foreach ($langues as $key => $value) {
        $validation["titrel".$value->id]='max:255';
        $validation["descriptionl".$value->id]='max:1000';
        $validation["champ1l".$value->id]='max:200';
        $validation["champ2l".$value->id]='max:800';
    }
    $this->validate($request, $validation);


    DB::beginTransaction();


        try{
            // vérifier upload file sur Laravel
        $file=$request->file('upload');
        $path=storage_path('app/public/'.$categorie->libelle);
        if(!Filemgr::exists($path)) {
            Filemgr::makeDirectory($path.'/mini', 0766, true);
            Filemgr::makeDirectory($path.'/micro', 0766, true);
        }

        Image::make($file)
            ->resize(1400, null, function ($constraint) {
                $constraint->aspectRatio();
            })
            ->save($path.'/'.$article->url);
        Image::make($file)
            ->resize(900, null, function ($constraint) {
            $constraint->aspectRatio();
            })
            ->save($path.'/mini/'.$article->url);
        Image::make($file)
            ->resize(600, null, function ($constraint) {
                $constraint->aspectRatio();
            })
            ->save($path.'/micro/'.$article->url);


        foreach($langues as $key=>$value){
            $text=TextArticle::firstOrNew(['idlangue' => $value->id,'idarticle'=>$id]);
            $text->titre=$request->input('titrel'.$value->id);
            $text->description=$request->input('descriptionl'.$value->id);
            $text->save();
        }

        DB::commit();
        }
        catch(Exception $e){
            DB::rollBack();

        }
        $categorie=Categorie::find($article->idcategorie);
    return redirect("/categorie/".$categorie->libelle);


}

在你的控制器中这样做:

public function update(Request $request, $id){
    $article = Article::find($id);
    $categorie = Categorie::find($article->idcategorie);
    if($article == null)
        return redirect("/home");
    $validation=[];
    $langues=Langue::all();
    foreach ($langues as $key => $value) {
        $validation["titrel".$value->id]='max:255';
        $validation["descriptionl".$value->id]='max:1000';
        $validation["champ1l".$value->id]='max:200';
        $validation["champ2l".$value->id]='max:800';
    }
    $this->validate($request, $validation);


    DB::beginTransaction();


        try{
            // vérifier upload file sur Laravel
        $file=$request->file('upload');
        $path=storage_path('app/public/'.$categorie->libelle);
        if(!Filemgr::exists($path)) {
            Filemgr::makeDirectory($path.'/mini', 0766, true);
            Filemgr::makeDirectory($path.'/micro', 0766, true);
        }

        Image::make($file)
            ->resize(1400, null, function ($constraint) {
                $constraint->aspectRatio();
            })
            ->save($path.'/'.$article->url);
        Image::make($file)
            ->resize(900, null, function ($constraint) {
            $constraint->aspectRatio();
            })
            ->save($path.'/mini/'.$article->url);
        Image::make($file)
            ->resize(600, null, function ($constraint) {
                $constraint->aspectRatio();
            })
            ->save($path.'/micro/'.$article->url);


        foreach($langues as $key=>$value){
            $text=TextArticle::firstOrNew(['idlangue' => $value->id,'idarticle'=>$id]);
            $text->titre=$request->input('titrel'.$value->id);
            if($request->input('champ1l'.$value->id)!=null&&$request->input('champ1l'.$value->id)!=''){
            $text->description=$request->input('champ1l'.$value->id).';'.$request->input('champ2l'.$value->id);
            }
            else{
            $text->description=$request->input('champ2l'.$value->id);
            }
            $text->save();
        }

        DB::commit();
        }
        catch(Exception $e){
            DB::rollBack();

        }
        $categorie=Categorie::find($article->idcategorie);
    return redirect("/categorie/".$categorie->libelle);


}