Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ralphdns's avatar

my search query is not displaying posts results 100%.

error1: Undefined property: stdClass::$user (View: C:\xampp\htdocs\infoblog\resources\views\page\search.blade.php)

error2: Undefined property: stdClass::$photo (View: C:\xampp\htdocs\infoblog\resources\views\page\search.blade.php)

see the SearchPostsController@store:


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);
       
    //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 search.blade.php view, which shows the search result

@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 my route:

Route::resource('page/search', 'SearchPostsController'); ```

THANKS ALOT
0 likes
5 replies
NOMGUY's avatar
NOMGUY
Best Answer
Level 16
$post->photo()->cover_image
$post->user()->name
sujancse's avatar

You wrote raw query $posts = DB::table('posts')->where('title', 'LIKE', '%' . $query . '%')->paginate(10); but trying to access Eloquent https://laravel.com/docs/5.6/eloquent-relationships relation.

If you have a model Post with two relations photo and user then try

$posts = Post::with('user','photo')->where('title', 'LIKE', '%' . $query . '%')->paginate(10);

Everything will work just fine.

ralphdns's avatar

@numguy. didnt work

error: Call to undefined method stdClass::photo() (View: C:\xampp\htdocs\infoblog\resources\views\page\search.blade.php)

ralphdns's avatar

thanks guys. both options worked. Just my BAD....I forgot to include namespace

Please or to participate in this conversation.