jet
278
2
Laravel

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'order clause'

Posted 6 months ago by jet

posts controller

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        if (request()->has('t') && request()->t == 'p') {
            $posts = Post::where('type', 'page')->orderBy('name', 'asc')->paginate(20);
            $view = 'page';
        }
        else {
            $posts = Post::where('type', 'post')->orderBy('name', 'asc')->paginate(20);
            $view ='post';
        }

        return view('posts.index', ['posts' => $posts, 'view' => $view ]);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $data = $request->input();
        unset($data['_token']);
        $data['user_id'] = Auth::id();

        $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)
    {

        $data = $request->input();      
        unset($data['_token']);
        unset($data['_method']);
        $data['user_id'] = Auth::user()->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');
    }
}

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

Reply to

Use Markdown with GitHub-flavored code blocks.