guys i am working on laravel project (i am beginner in laravel ) my role in this project is making the views and injecting the data ... while i am trying to inject Post in my view i got this error "Trying to get property of non-object " i am searching on the internet and some says that "may be the object returns null but i checked all the data comes from the object in the Controller and i found data by dd($post);"
here's the HomeController method that returns the post object
public function index()
{
$sliders = Slider::take(5)->get();
$post = Post::latest()->first();
$users = User::subscribed()->take(12)->get()->shuffle();
return view('frontend.pages.home2', compact('sliders', 'sliders', 'users', 'post'))->with('success',' success test');
}
and this is what i am trying to do in the view
@foreach($post as $pos)
<p class="text-primary col-lg-4 col-md-4 sliderParagraph menuDirection">
{{$pos->body_ar}}
</p>
@endforeach
<p class="text-primary col-lg-4 col-md-4 sliderParagraph menuDirection">
some text here to be displayed
<br>
<button type="button" class=" btn btn-primary">more</button>
</p>
<img class="nlpimage img-responsive " src="images/nnn.jpg">
</div>
and here's the Post.php class
namespace App\Src;
use App\Core\Traits\LocaleTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class Post extends Model
{
use Notifiable, LocaleTrait;
public $localeStrings = ['title', 'body'];
public $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function gallery()
{
return $this->morphMany(Gallery::class, 'galleryable');
}
}
and the post table in the DB enter image description here