vendredi 1 septembre 2017

Invalid argument supplied for foreach() Laravel tags

I just make tags system for my blog using many to many relationship in laravel. Now I am trying to add multitag but its getting error:

Invalid argument supplied for foreach()

here is my controller

public function blogpost(Request $request){
    $this->validate($request, [
        'title' => 'required|max:255|unique:blogs',
        'content' => 'required',
        'image' => 'image|required',
    ]);
    $blog = new blog;
    $blog->title = $request->title;
    $blog->content = $request->content;
    $blog->slug = str_slug($blog->title, '-');
    $blog->user_id = Auth::user()->id;
    $blog->published_at = $request->published_at;
    if($request->hasFile('image')) {
        $file = Input::file('image');
        //getting timestamp
        $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
        $name = $timestamp. '-' .$file->getClientOriginalName();
        $file->move(public_path().'/images/blog/', $name);
        $blog->image = $name;
        $thumb = Image::make(public_path().'/images/blog/' . $name)->resize(640,420)->save(public_path().'/images/blog/thumb/' . $name, 90);
    }

    $blog_id = $blog->id;
    $tags = $request->tag;

    foreach ($tags as $tag) {
        $tag = tag::firstOrNew(['name' => $tag]);
        dd($tag);
    }

    return Redirect::back()->with('status', 'Post Success');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire