2016-06-20 7 views
0

私はかなり新しいlaravelです。私はリダイレクト経由で私のビューにいくつかのデータを送ろうとしています。私のコントローラでLaravelはリダイレクト経由で私のビューに配列を渡します

:これは私のコードは次のようになります今

$tags = Tag::all(); 
return Redirect::to('klant/profile/edit')->with('tags', $tags); 

私の見解では、私は、選択フィールド内のすべてのタグをループしたいです。私はそうのように、この操作を行います。

<select name="filterTags" class="form-control" id="tagsDropdown"> 
    <option value="all">Alle projecten tonen</option> 
    @foreach (Session::get('tags') as $tag) 
     <option value="{{ $tag->name }}">{{ $tag->name }}</option> 
    @endforeach 
</select> 

しかし、私はエラーを取得する:

"invalid argument supplied for foreach"

は、誰も私を助けることはできますか?

ご協力いただきましてありがとうございます。事前に多くの感謝!

@if ($tags->count()) 
    @foreach ($tags as $tag) 
     ... 
    @endforeach 
@else 
    No tags 
@endif 

この方法でリダイレクトを使用しない:あなたがタグを持っていないとき、あなたのビューでこれをしようとこのエラーを回避するために

+2

これは良い方法ではありません。 'klant/profile/edit'を扱う関数で' $ tags'を取り出してみませんか? – ceejayoz

+0

フラッシュされた 'tags'データがないと、エラーがスローされます。おそらく、パラメータの存在を確認する必要がありますが、なぜEloquentモデルが点滅していますか?それは珍しいことです。 – undefined

+0

あなたのlaravelのバージョンは何ですか?そして、dd(Session)を試しましたか? – Mojtaba

答えて

1
public function index(){ 
    $tags = Tag::all(); 
    return view('welcome',compact('tags')) 
} 

ちょうどあなたがあなたの代わりにコンパクトなのもそれを使用することができwith()機能を使用するためにwan't場合は、あなたのresources/views/ディレクトリ

welcome.blade.phpというページをしましたことを確認してください。

return view('welcome')->with('tags','other_variables'); 
0

。私はそれを説明しようとする例を書いたなど、インデックスページに戻るには、エラーとして応答を返すためにそれを使用し、ビューを表示するためにリダイレクトを使用しないでください。

あなたのインデックスメソッドは次のようにする必要があります

public function index() 
{ 
    // find your object 
    $tags = Tag::all(); 

    // return the view with the object 
    return View::make('tags.index') 
     ->with('tags', $tags) 
} 

このようでなければならないようなあなたの編集:Laravelのドキュメントで

public function edit($id) 
{ 
    // find your object 
    $tag = Tag::find($id); 

    // if tag doesn't exist, redirect to index list 
    if (!tag) 
    { 
     return Redirect::route('tags.index') 
      ->with('message', "Tag doesn't exist") 
    } 

    // return the view with the object 
    return View::make('tags.edit') 
     ->with('tag', $tag) 
} 

より多くの例:https://laravel.com/docs/5.2/responseshttps://laravel.com/docs/5.2/views

関連する問題