jet

Undefined variable: categories (View: E:\lara-test1\resources\views\posts\create.blade.php)

Posted 1 week ago by jet
@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

post controller

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Post;
use App\Category;
use Session;


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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, array(
                'title' => 'required|max:255',
                
                
        ));

        $post = new Post;

        $post->title = $request->title;
        $post->category_id = $request->category_id;
        $post->content = $request->content;
        $post->order = $request->order;
        $post->views = $request->views;
        $post->status = $request->status;
        $post->slug  = str_slug($request->title);
        $post->meta = $request->meta;
        

        $post->save();

        Session::flash('success', 'sucessfully save!');
        return redirect()->route('posts.index', $post->id);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $post = Post::find($id);
        return view('posts.show')->withPost($post);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $post = Post::find($id);
        $categories = Category:: all();
        $cats = array();
        foreach ($categories as $category) {
            $cats[$category->id] = $category->name;
        }

        return view('posts.edit', ['post' => $post, 'categories' => $cats]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         $this->validate($request, array(
                'title' => 'required|max:255'            
        ));

         $post = Post::find($id);

        $post->title = $request->input('title');
        $post->category_id = $request->input('category_id');
        $post->content = $request->input('content');
        $post->order = $request->input('order');
        $post->views = $request->input('views');
        $post->status = $request->input('status');
        $post->slug  = str_slug($request->input('title'));
        $post->meta = $request->input('meta');

        $post->save();

        Session::flash('success', 'This post was successfully updated');

        return redirect()->route('posts.show', $post->id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
         $post = Post::find($id);

        $post->delete();

        Session::flash('success', 'The post was sucessfully deleted.');
        return redirect()->route('posts.index');
    }
}

will be glad if any one can 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.