I'm creating a small social media where admin can posts to user's dashboard but having trouble with showing the admin details like profile picture to the posts
this is the error i got
Trying to get property 'profile_image' of non-object
Check my code
Post.php // my post model
public function admin()
{
return $this->belongsTo('App\Admin');
}
Admin.php // my admin model
public function post()
{
return $this->hasMany('App\Post');
}
now on my user dashboard i would try to access my admin details using this code {{ $post->admin->profile_image }}
You get that error because $post->admin is null You must be check $post->admin empty or not
{{ $post->admin ? $post->admin->profile_image : ''}}
Related
I am trying to get Store details, belong to a specific user in blade template
getting this error:
"ErrorException Trying to get property 'name' of non-object"
I can get all details in User table, error comes when i am trying to get store table data.
#forelse ($users as $user)
<span class="col-span-1 px-2 py-1 border">{{ $user->store->name }} </span>
#endforeach
User Model
public function store()
{
return $this->hasOne('App\Models\store');
}
store Model
public function user()
{
return $this->belongsTo('App\Models\User' );
}
It works when i added below code to the livewire component class
use Illuminate\Database\Eloquent\Model;
As a project I am building a stackoverflow like forum. On the page on which a single question is shown I want the user to be able to click on the questioner's name and be forwarded to the respective user profile page. I am able to get the name from the database with {{ $question->user->name }}. The problem occurs when adding the part!
Also, the profile pages work. I can access them and the url then says for example: ../profile/1.
This is the route in the web.php file:
Route::get('/profile/{user}', 'PageController#profile')->name('profile');
This is the PageController part:
public function profile($id)
{
$user = User::with(['questions', 'answers', 'answers.question'])->find($id);
return view('profile')->with('user', $user);
}
This is the code from the show.blade View question page which does not work:
<p>
Submitted by {{ $question->user->name }}
</p>
The error message I get is Undefined variable: user.
Which surprises me because forwarding on the profile page to a specific question works with this blade code:
View Question
The respective route in the web.php file:
Route::resource('questions', 'QuestionController');
And QuestionController:
public function show($id)
{
$question = Question::findOrFail($id);
return view('questions.show')->with('question', $question);
}
I thought I defined the variable $user in the PageController like I defined $question in the QuestionController?
I can see you are using Eloquent models with relations. If you want to display the user id on the question, you can use the relation between the Question and User to find the id of the posting user.
Submitted by {{ $question->user->name }}
^^^^^^^^^
//just change your like this way
public function profile($id)
{
$user = User::with(['questions', 'answers', 'answers.question'])->find($id);
return view('profile',compact('user));
}
I want to create a feature which the post are require approve by 5 admin in order to display. When a post is created, the post will store in Posts table. The post by default the approved column is false. In order to display the post , it require 3 approval out of 5 admin. So i want get the post from database and show in admin panel. Each time will randomly show 1 post for the admin. If admin approved the post.The approval is storing in other table name Approvals.
The model is
Post model
public function user(){
return $this->belongsTo('App\User');
}
public function post(){
return $this->belongsTo('App\Forum\Post');
}
In approval model
protected $fillable = [
'admin_id',
'post_id',
'approved'
];
public function admin(){
return $this->belongsTo('App\Admin');
}
public function post(){
return $this->belongsTo('App\Forum\Post');
}
In my controller i am using this to get random unapproved post. But now i need to filter out the post, if approval table are contain the approval data by one of the admin. He/she will not see the post again.
$post = Post::inRandomOrder()->where('approved', false)->first();
In case of it might having many post to approve. How can i know that the admin is approved the post so the same post will not showing again to the same admin.
Your post() relationship in your Post model seems like a bug. I think it should be a relationship with your Approval model, right? So first, in your Post model, replace the post() method with:
public function approvals() {
return $this->hasMany('App\Forum\Approval');
}
Then, here's one approach for your query:
$post = Post::inRandomOrder()->where('approved', false)
->with('approvals')
->get()
->filter(function($value, $key) {
// A post without any approvals yet is OK
if ($value->approvals->count() === 0) {
return true;
}
// If it has approvals, check the current admin isn't one of them
return !$value->approvals->contains('admin_id', \Auth::user()->id);
})
->first()
I've created a resource
Route::resource('page-category', 'PageCategoryController',['except'=>['create'] ]);
code in view:
Edit
and my edit method in PageCategoryController.php
public function edit($id)
{
$pcategory = PageCategory::find($id);
return view('admin.page-category.show')->withPcategory($pcategory);
}
when i click the button in the view it redirectes to my 404 view. When i hover over the button the link is like http://localhost:8000/page-category//edit . When i manually insert id number in link http://localhost:8000/page-category/1/edit it does takes me to edit page.
Try this according to routing guide on https://laravel.com/docs/5.4/controllers#resource-controllers
Edit
What if your controller becomes:
public function edit($id) {
$pcategory = PageCategory::find($id);
return view('admin.page-category.show')->compact('pcategory');
}
So this will send the $pcategory, in this case your page category with the id ($id), to your view.
From there you'll be able access to your route page-category.edit by sending it the id of the page category: $pCategory->id just like you did.
I'm using Laravel 5.2. In my application, if a user is an admin, he can see all groups. Otherwise, he can only see his groups.
Model
public function groups() {
if ($this->isAdmin()) {
return \App\Group::get();
}
return $this->belongsToMany('App\Group');
}
View
#foreach($user->groups as $group)
{{ $group->name }}
#endforeach
Result
The code above works if the user is not an admin, but I get this error if the user is an admin
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
I tried this instead: $user->groups() as $group, it works when the user is an admin, but show nothing when it's not an admin.
Question
I know when I call a relation as a property ($user->groups), it returns a collection of objects. Instead, if I call it as a function ($user->groups()), I get a QueryBuilder instance.
What can I do to use the same syntax as in my view ?
Note: I cannot add all groups in the database to the admin, as admins must have no group.
The way is not using directly relationship for this but using extra method to handle this.
First in your model create simple relationship:
public function groups()
{
return $this->belongsToMany('App\Group');
}
and then create extra method:
public function availableGroups()
{
if ($this->isAdmin()) {
return \App\Group::get();
}
return $this->groups;
}
Now in view you can use:
#foreach($user->availableGroups() as $group)
{{ $group->name }}
#endforeach