jet

Invalid argument supplied for foreach() (View: E:\lara-test\resources\views\posts\create.blade.php)

Posted 1 month ago by jet

this is the create form

@extends('main')

@section('title', '| Ceate New Post')

@section('content')

@section('content')
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h1>Create New Post</h1>
            <hr>
            <form name="ceate" action="/posts"  method="post">
                {{ csrf_field() }}
                <div class="form-group">
                    <label for="title">Titel</label>
                    <input type="text" name="title" required id="title" class="form-control" placeholder="Titel">
                </div>

                <div class="form-group">
                    <label for="order">Order</label>
                    <input type="text" name="order" required id="order" class="form-control" placeholder="Order">
                </div>
                <div class="form-group">
                    <label for="category_id">Category</label>
                        <select class="custom-select" name="category_id" id="category_id"  required> 
                            @foreach($categories as $category)
                                <option value='{{ $category->id }}'>{{ $category->name }}</option>
                            @endforeach
                        </select>
                </div>
                <div class="form-group">
                    <label for="content">Content</label>
                    <textarea name="content" id="content" required class="form-control" rows="5" placeholder="Content"></textarea>
                </div>

                <div class="form-group">
                    <label for="status">Status</label>
                        <select class="custom-select" name="status" id="status" required>
                          <option value="1">One</option>
                          <option value="2">Two</option>
                          <option value="3">Three</option>
                        </select>
                </div>  
                
                <br>
                
                <input class="btn btn-success btn-lg btn-block" type="submit" value="Create post">
            </form>
        </div>
    </div>
@endsection

the category controller

class CategoryController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $categories = Category::all();
       
        return view('categories.index')->withCategories($categories);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //Save a new category an then redirect back ti index
        $this->validate($request, array(
            'name' => 'required|max:255'
        ));
         $category = new Category;

         $category->name = $request->name;
         $category->save();

         Session::flash('success', 'New Category has been created');

         return redirect()->route('categories.index');
    }

the post controller form

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
     
    public function index()
    {
        $posts = Post::orderBy('id', 'desc')->paginate(10);
        return view('posts.index')->withPosts($posts);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Category::all(); 
        return view('posts.create')->withCategories('$categories'); 
    }

    /**

please i need help

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.