lundi 29 juillet 2019

Undefined property: stdClass::$user AND Call to a member function diffForHumans() on string

When i run my webpage it's can run normally but i can't search. I need to search for data by those data from different table and when i search data it show error

Undefined property: stdClass::$user (View: C:\xampp\htdocs\ff\Laravel-Webboard-Workshop-master\resources\views\group\index.blade.php)

and created_at->diffForHumans() too it show error:

Call to a member function diffForHumans() on string (View: C:\xampp\htdocs\ff\Laravel-Webboard-Workshop-master\resources\views\group\index.blade.php)

$group is in table groups no problem.

$group->user->name is about table users has problem:

Undefined property: stdClass::$user

and $group->created_at->diffForHumans() has problem:

Call to a member function diffForHumans() on string

view group\index.blade.php

<div class="container">
  <div class="row">

    <div class="col-md-12">
      <form action="/search" method="get">
        <div>
          <button type="submit" class="btn btn-primary" style="float:right;">Search</button>
          <a style="float:right;">
            <input type="search" name="search" class="form-control" >
          </a>
        </div>
      </form>
    </div>
    <br><br>
    <div class="col-md-12">
      <div class="panel panel-default">
        <div class="panel-heading" style="font-size: 18px;">Home</div>
        <div class="panel-body">
          <table class="table table-striped table-linkable table-hover">

            <thead>
              <tr>
                <th class="text-center">Group</th>
                <th class="text-center">Posted By</th>
                <th class="text-center">Posted At</th>

              </tr>
            </thead>

            <tbody>
              @foreach($groups as $group)
                <tr onclick="document.location.href = ''">
                  <td></td>
                  <td class="text-center"></td>                     <!--error for search-->
                  <td class="text-center"></td>    <!--error for search-->
                </tr>
              @endforeach
            </tbody>

          </table>
          <div class="text-center"> {!! $groups->links(); !!} </div>
        </div>
      </div>
    </div>

  </div>
</div>


Controller GroupController.php

class GroupController extends Controller
{
   public function search(Request $request)
   {
     $search = $request->get('search');
     $groups = DB::table('groups')->where('title', 'like', '%'.$search.'%')->paginate(5);
     //$users = DB::table('users')->where('name', 'like', '%'.$search.'%')->paginate(5);

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


Route

Route::get('/search','GroupController@search');

How should I fix it ??



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire