Home > Back-end >  Trying to show currently authenticated user post in laravel view
Trying to show currently authenticated user post in laravel view

Time:07-24

I am working on a Laravel project and I want to display currently authenticated users posts in view file, I am getting an error while trying to achieve that says

"Class name must be a valid object or a string"...

my blade view

@extends('layouts.app')

@section('content')
<div >
    <div >
        <div >
            <div id="accordion">
  <div >
    <div  id="headingOne">
      <h5 >
        <button  data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        <i  aria-hidden="true"></i> Swap 
        </button>
      </h5>
    </div>

    <div id="collapseOne"  aria-labelledby="headingOne" data-parent="#accordion">
      <div >
                  
      @foreach($data as $dat)
            <div  style="max-width: 540px;">
  <div >
    <div >
      <img
        src="{{asset('images/' . $dat->imogi)}}"
        alt="Trendy Pants and Shoes"
      />
    </div>
    <div >
      <div >
      <div style="text-align: right;">
        <small style="color:#3490dc;">{{$dat->organazation}}</small> <small><i  style="color:#3490dc" aria-hidden="true"></i> {{ $dat->created_at->format('d/m/Y') }}</small>
      </div>
      <h5 ><i  style="color:#3490dc" aria-hidden="true"></i> {{$dat->name}}</h5>
      <small><i  style="color:#3490dc" aria-hidden="true"></i> {{$dat->location}}</small> | <small><i  style="color:#3490dc" aria-hidden="true"></i>{{$dat->contact}}</small>
      <hr/>
        <p >
        <b>{{$dat->title}}</b>
        </p>
        <small style="color:#3490dc;">M{{$dat->price}}</small>
        <p >
        {{$dat->message}}
        </p>
      </div>
    </div>
  </div>
</div>
<br/>
            @endforeach
      </div>
    </div>
  </div>
  <div >
    <div  id="headingTwo">
      <h5 >
        <button  data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
        <i  aria-hidden="true"></i> Work 
        </button>
      </h5>
    </div>
    <div id="collapseTwo"  aria-labelledby="headingTwo" data-parent="#accordion">
      <div >
                    <form  type="get" action="{{url('/search_work')}}" role="search" method="get">
                    <input id="search-input" type="search" name="query"  placeholder="Search..."> 
                    <button type="submit" ><i  aria-hidden="true"></i></button> 
                    </form>

                    <br/>
      <p style="text-align:center">
      <button onclick="window.location.href='/workview'" type="button" >View</button><small style="color:#3490dc"> {{$count = DB::table('jobs')->count()}}</small> <small>Posts</small>
    </p>
      </div>
    </div>
  </div>
  <div >
    <div  id="headingThree">
      <h5 >
        <button  data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
        <i  aria-hidden="true"></i> Sell 
        </button>
      </h5>
    </div>
    <div id="collapseThree"  aria-labelledby="headingThree" data-parent="#accordion">
      <div >
                    <form  type="get" action="{{url('/search_sell')}}" role="search" method="get">
                    <input id="search-input" type="search" name="query"  placeholder="Search...">
                    <button type="submit" ><i  aria-hidden="true"></i></button>  
                    </form>

                    <br/>
      <p style="text-align:center">
      <button onclick="window.location.href='/sellview'" type="button" >View</button><small style="color:#3490dc"> {{$count = DB::table('announces')->count()}}</small> <small>Posts</small>
    </p>
      </div>
    </div>
  </div>
  <div >
    <div  id="headingFour">
      <h5 >
        <button  data-toggle="collapse" data-target="#collapseFour" aria-expanded="false" aria-controls="collapseFour">
        <i  aria-hidden="true"></i> Tenders 
        </button>
      </h5>
    </div>
    <div id="collapseFour"  aria-labelledby="headingFour" data-parent="#accordion">
      <div >
                    <form  type="get" action="{{url('/search_tender')}}" role="search" method="get">
                    <input id="search-input" type="search" name="query"  placeholder="Search...">  
                    <button type="submit" ><i  aria-hidden="true"></i></button>
                    </form>

                    <br/>
      <p style="text-align:center">
      <button onclick="window.location.href='/tenderview'" type="button" >View</button><small style="color:#3490dc"> {{$count = DB::table('Kinds')->count()}} </small><small>Posts</small>
    </p>
      </div>
    </div>
  </div>
</div>
</div>
    </div>
</div>
<br/>
@include('footer')
@endsection 

My controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use DB;

class PostController extends Controller
{
    //
    public function mapost()
    {

       
        $data = Auth::user()->Announce::orderBy('created_at', 'DESC')->get();

        return view('users.mapost',['data'=>$data]);
    }
}

My route in web.php

Route::get('users/mapost', 'PostController@mapost')->name('users.mapost');

I don`t know where I am getting this wrong, i will appreciate any help thanks in advance

CodePudding user response:

Look at this line:

$data = Auth::user()->Announce::orderBy('created_at', 'DESC')->get();

Here you attempt to call the orderBy method of the Auth::user()->Announce class and of course, such class does not exist. It would make sense to do something like this:

Announce::where('user_id', $youruserid)->orderByDesc('created_at');
  • Related