Relationship In laravel Undefined property: stdClass:: error - laravel

Hi I am new to laravel and eloquent relationships I am getting this error when i try to display the category name of the formation
Undefined property: stdClass::$category (View: E:\khibra-platfrom\resources\views\formation\index.blade.php)
Here is my category model
class Category extends Model
{
function formations()
{
return $this->hasMany('App\Formation');
}
protected $fillable =['name','description'];
}
And my Formation model
class Formation extends Model
{
function category()
{
return $this->belongsTo('App\Category',"category_id");
}
}
I am trying to get category name for each formation like this
#foreach($formations as $formation)
<tr>
<th scope="row">{{$formation->id}}</th>
<td>{{$formation->name}}</td>
<td>{{$formation->price}}</td>
<td>{{$formation->category->name}}</td>
<td>{{$formation->durations}}</td>
<td><div class="row">
</tr>
#endforeach
This is my Controller code
{
//$formations = Formation::all();
$formations = DB::table('formations')->paginate('4');
$categories = Category::all();
return view('formation.index',compact('categories','formations'));
}
Can any one help ?

You are use DB facade not eloquent model
$formations = DB::table('formations')->paginate('4');
Change this to
Formation::with('category')->paginate(4);
also in model edit like this
class Formation extends Model
{
public function category()
{
return $this->belongsTo('App\Category',"category_id");
}
}
in blade change code to
<td>{{$formation->category->name ?? ''}}</td>
For avoid error exception when one formation is not have category

Related

Trying to get property 'first_name' of non-object

I am trying to fetch digitizing order related to the user but unfortunately, I am facing an error.
Please see this error: https://flareapp.io/share/VmeWJ47Q
Controller
public function index()
{
$data=
[
'digitizings'=>Digitizing::with('user')->paginate(8)
];
return view('front_end.profile.digitizing.digitizing_view_order',$data);
}
User Model
class User extends Authenticatable
{
use Notifiable;
public function digitizing()
{
return $this->hasMany('App\Digitizing','user_id');
}
}
Digitizing Model
class Digitizing extends Model
{
protected $fillable = ['id','order_name','height','width','urgent','image',
'order_placement','required_format','order_fabric','instruction','user_id'];
protected $table ="digitizing_orders";
public function user()
{
return $this->belongsTo('App\User');
}
}
HTML view
#foreach($digitizings as $key =>$digitizing)
<tr>
<td>DPO# {{$digitizing->id}}</td>
<td>{{$digitizing->created_at}}</td>
<td>{{$digitizing->order_name}}</td>
<td>{{$digitizing->user->first_name}}</td>
<td>{{$digitizing->user->email}}</td>
<td>{{$digitizing->released_date ?? 'processing'}}</td>
<td>View
</td>
</tr>
#endforeach
Does every Digitizing-Entry has set a valid user_id in database? Try checking eager loaded data is set before accessing it.
Try to send the data to the view like this:
public function index()
{
$digitizings = Digitizing::with('user')->paginate(8);
return view('front_end.profile.digitizing.digitizing_view_order',$digitizings);
}

Trying to get property 'name' of non-object?

Hi Guys i am new to laravel and i am trying to retrieve category names for each formation and i am getting this error
Trying to get property 'name' of non-object (0)
The "name" i am trying to retrieve is the name of category that my formation belongs to.
This is My Category Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
function formations()
{
return $this->hasMany('App\Formation');
}
protected $fillable =['name','description'];
}
And This is My Formation Model
class Formation extends Model
{
function category()
{
return $this->belongsTo('App\Category',"category_id");
}
protected $fillable =['name','price','durations','category_id'];
}
And here is my view page where i am trying to retrieve the category name
#foreach($formations as $formation)
<td>{{$formation->name}}</td>
<td>{{$formation->price}}</td>
<td>{{$formation->category->name}}</td>
td>{{$formation->durations}}</td>
#endforeach
This is My Controller
public function index()
{
//$formations = Formation::all();
$formations = Formation::with('category')->paginate('4');
$categories = Category::all();
return view('formation.index',compact('categories','formations'));
}
Any Suggestion about where i might be wrong ? And thanks in Advance !
This error usually comes when you have removed one or more row from database, but trying to access that row with an id. Make sure that you have all the category in the category table with the id which is mentioned in the formation table category_id field.
In your view
#foreach($formations as $formation)
<td>{{$formation->name}}</td>
<td>{{$formation->price}}</td>
<td> {{ $formation->categories()->pluck('name')->implode(' ') }}</td>
<td>{{$formation->durations}}</td>
#endforeach
Try changing the relationship
class Formation extends Model
{
function category()
{
return $this->belongsTo('App\Category',"id","category_id"); //if your categories id is id
}
protected $fillable =['name','price','durations','category_id'];
}
In your view
#foreach($formations as $formation)
<td>{{$formation->name}}</td>
<td>{{$formation->price}}</td>
<td>{{$formation->category->name}}</td>
<td>{{$formation->durations}}</td>
#endforeach
The Problem was that i have deleted a row from my category table in database that's why the name wasn't recognized.

cannot use hasOne relationship to place restriction

i have hasone relationship between models "member" and "loan". i throws "trying to get property of non object error
i have tried on hasMany and it all works but it has to be in hasOne, there is no restriction to stop multiple loan being issued to a member.
Member.php
use Illuminate\Database\Eloquent\Model;
class Member extends Model
{
protected $fillable=['name','address','phone'];
public function loan()
{
return $this->hasOne(Loan::class,'member_id','id');
}
public function savings()
{
return $this->hasMany(Saving::class,'member_id','id');
}
}
Loan.php
use Illuminate\Database\Eloquent\Model;
class Loan extends Model
{
protected $fillable = [
'amount',
'interest',
'status',
'member_id',
'loan_type_id',
'interest_type_id',
'loan_payment_type_id'
];
public function member()
{
return $this->belongsTo(Member::class,'member_id','id');
}
}
member.blade.php
<td>{{$member->name}}</td>
<td>{{$member->address}}</td>
<td>{{$member->phone}}</td>
<td>
Delete
Edit
</td>
<td>
{{$member->loan->amount}}
</td>
Maybe all of your member don't have a loan. that's why error shown, Try to check before echo,
<td>
#if($member->loan) // add this line code
{{ $member->loan->amount }}
#endif // add this line code
</td>
Try
{{$member->loan['amount']}}

laravel 5.7 Relationships belongsto hasmany error

I'm new to laravel, I'm beginning with laravel 5.7. I have two model.
Modelo categoria:
class categoria extends Model
{
public function indicadores()
{
return $this->hasMany('App\Indicador');
}
}
Modelo Indicador:
class Indicador extends Model
{
public function categoria()
{
return $this->belongsTo('App\categoria');
}
}
In the controller, I look for all Indicador
class IndicadorController extends Controller {
public function index() {
$indicadores = DB::table('indicadors');
return view('indicador.index', ['indicadores' => $indicadores]);
}
When I try to show the category to which the indicador belongs,
<tbody>
#foreach ($indicadores as $indicador)
<tr>
<td>{{ $indicador->categoria->nombre }}</td>
</tr>
#endforeach
</tbody>
I get the following error
Undefined property: stdClass::$categoria (View:resources\views\indicador\index.blade.php)
And I don't understand why. Thanks in advance
Using the DB facade returns an instance of Illuminate\Database\Query\Builder
so the relationship will not be accessible.
You instead want an Eloquent model instance which you can use the model directly:
use App\Indicador;
...
$indicadores = Indicador::all();
or as #Chris suggests, eager load:
$indicadores = Indicador::with('categoria')->get();
Use Eloquent instead of the query builder:
class IndicadorController extends Controller {
public function index() {
$indicadores = Indicador::all();
return view('indicador.index', ['indicadores' => $indicadores]);
}
}

Access user's properties (with pivot table) laravel 5.1

I have a users table. The user has possibility to upload a post. The post is saved well to the database. The users can follow each other - so on a pivot table each follower_id has followees_id.
I need to get the posts of the current user followee's . I am kinda messed with getting it from a pivot table.
Here's my code so far:
controller:
protected function get_followee_posts($followee_posts) ////$followee_posts passed from the route.
{
$user = $this->user;
$user->followee()->attach($followee_posts);
$followee_posts = User::find($followee_posts);
}
view:
<div class="following_posts">
<p> Posts from people you're following: <p>
#foreach ($user->follower_id->followee_id->posts as $post)
<form action="/html/tags/html_form_tag_action.cfm" method="post">
<textarea name="comments" id="comments" style="width:96%;height:90px;background-color:white;color:black;border:none;padding:2%;font:22px/30px sans-serif;">
{!! $posts->full_post !!} </textarea>
</form>
#endforeach
route:
Route::get('hub/{followee_posts}','HubController#get_followee_posts')->name('followee_posts');
I am getting an error with the current code saying:
ErrorException in 7340f90cc5faf1a298fcc646b7248b22 line 105:
Trying to get property of non-object
Any help would be lovely. Thank you.
Your not too specific about your schema, but this is how I would go about it.
User Model
class User extends Model
{
protected $table = 'users';
public function posts()
{
return $this->hasMany('Post');
}
public function followers()
{
return $this->hasMany('Follower');
}
}
Follower Model
class Follower extends Model
{
protected $table = 'followers';
public function user()
{
return $this->belongs_to('User');
}
public function posts()
{
return $this->hasMany('Post', 'user_id', 'follower_id');
}
}
Post Model
class Post extends Model
{
protected $table = 'posts';
public function user()
{
return $this->belongs_to('User');
}
}
The followers table would look something like this:
user_id
follower_id
You could then use eloquent method chains to get the posts of a users followers:
// Get Users object with followers and followers posts
// We use with() to eager load relationships
$user = User::with('followers.posts')->find(2);
// Return associative array of post objects
$postsArray = $user->followers->lists('posts');
// Combine posts into a single collection
$posts = (new \Illuminate\Support\Collection($postsArray))->collapse()->toArray();
print_r($posts);

Resources