ralphdns's avatar

my SearchPostsController@store doesnt recognise my Post model

error message: Class 'Post' not found

For this reason, my Search engine is worthless...pls help me improve

see the SearchPostsController


namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
/* use Illuminate\Http\Request; */
use Request;
use Illuminate\Support\Facades\Input;
use\DB;
use\Post;

class SearchPostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
           //
        // Gets the query string from our form submission 
   $query = Request::input('search');

   // Returns an array of articles that have the query string located somewhere within 
   // our articles titles. Paginates them so we can break up lots of search results.
 /*   $posts = DB::table('posts')->where('title', 'LIKE', '%' . $query . '%')->paginate(10); */
   
    $posts =  Post::with('user','photo')->where('title', 'LIKE', '%' . $query . '%')->paginate(10); 
       
    //returns a view and passes the view the list of articles and the original query.
   return view('page.search', compact('posts', 'query'));
       
    }

    
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

see the view to render search results:

@extends('layouts.frontposts') <!--this contains doctype, jst b4 the yeild('content')-->

@section('content')

@include('includes.form_error')
 {{--    <h1>Posts</h1> --}}
    <!--if $post variable is countin frm table model in postsController, loop tru it, put in a well, then bring out the particular row u want-->
    @if(count($posts) > 0)
        <ul class="list-group">
            @foreach($posts as $post)
                <li class="list-group-item">
                    <div class="well">
                        <div class="row">
                            <div class="col-md-4 col-sm-4">
                                <img width="100%" src="/images/{{$post->photo ? $post->photo->cover_image : 'noimage.jpg'}}" alt="No image">
                            </div>
                            <div class="col-md-8 col-sm-4">
                                <h3><a href="{{route('front.posts.show', $post->id)}}">{{$post->title}}</a></h3>

                                <div>
                                    {!!$post->body!!}
                                </div>
                                <hr>

                                <small>Wrtten on {{$post->created_at}} by {{$post->user->name}}</small>
                            </div>
                        </div>
                    
                    </div>
                </li>
            @endforeach
        </u>
        {{$posts->links()}}<!--placin ur pagination links, using links() mtd-->
     @else
        <p>No Posts</p>
    @endif
@endsection

see the route for the search:

use Illuminate\Support\Facades\Input;


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//for search bar
Route::resource('page/search', 'SearchPostsController');

Route::get('/', function () {
    return view('welcome');
});


Route::group(['middleware'=>'admin'], function(){

    //render an admin pg witout view
    Route::get('/admin', function(){
        return view('admin.index');
    });

 });



//blog-post route, for show page. to render post page witout resource controller
Route::get('/post/{id}', ['as'=>'home.post', 'uses'=>'AdminPostsController@post']);

/* FRONT-posts. */
Route::resource('front/posts', 'FrontPostsController', ['as' => 'front']);


Route::resource('admin/posts', 'AdminPostsController', ['as' => 'admin']);
Route::resource('admin/users', 'AdminUsersController', ['as' => 'admin']);
Route::resource('admin/categories', 'AdminCategoriesController', ['as' => 'admin']);
Route::resource('admin/media', 'AdminPhotosController', ['as' => 'admin']);
Route::resource('admin/comments', 'CommentsController', ['as' => 'admin']);
Route::resource('admin/comment/replies', 'RepliesController', ['as' => 'admin']);

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
0 likes
1 reply
Abi's avatar
Abi
Best Answer
Level 24

Your post namespace might be wrong use {namespace here}\Post;

Please or to participate in this conversation.