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.
Related
Error like that...How can i solve that?
Country database
This is country model
This is Challenge database
This is Challenge model
#foreach ($challenges as $challenge)
<tr>
<td>{{$challenge->c_sport_id}}</td>
<td>{{$challenge->c_gender}}</td>
<td>{{$challenge->c_location}}
<td>{{$challenge->countries->country_name}}</td>
</tr>
#endforeach
change your relation
<?php
class Challenge extends Model
{
public function country()
{
return $this->belongsTo(\App\Country::class, 'c_country_id', 'id');
}
}
The issue in your Challenge Model. You have country id in your challenge model. So you should use relationship belongsTo instead of hasOne. So change your Challenge Model relationship like this:-
<?php
class Challenge extends Model
{
public function country()
{
return $this->belongsTo(\App\Country::class, 'c_country_id', 'id');
}
}
This is the correct one.
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
I have 3 model : store model, product model, order model
store model is like this :
class Store extends Model
{
protected $fillable = ['name', 'user', 'address', 'phones', 'email', 'photo'];
...
}
product model is like this :
class Product extends Model
{
protected $fillable = ['store_id','category_id','name', 'photo','description'];
...
}
order model is liket this :
class Order extends Model
{
...
public function products()
{
return $this->belongsToMany(Product::class, 'orders_products')
->withPivot('sub_total','quantity','price','information')->withTimestamps();
}
public function store()
{
return $this->belongsTo(Store::class);
}
}
I have 4 table : stores table, orders table, products table, orders_products table
orders_producs table is pivot table
In view blade laravel I call this :
{{ $order->store->name }}
It works. It success display store name
My problem is when I want to display product name
In view blade laravel I try :
{{ $order->products->name }}
There exist error :
Undefined property: Illuminate\Database\Eloquent\Collection::$id
(View:...
It seems the wrong way of calling
How can I solve it?
Since it's belongsToMany(), you can display data like this:
#foreach ($order->products as $product)
{{ $product->name }}
{{ $product->pivot->price }}
#endforeach
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);
I have the following table structure in my database.
Table Name: tiles
Columns: id, tile_name, tile_thumb, tile_snippet
Table Name: tags
Columns: id, tag_title
Table Name: tile_tags
Columns: id, tile_id, tag_id
Models:
Tile, Tag, TileTag
In my main model class for entries I am specifying the following relationship to a model called TileTag which is a pivot table.
<?php namespace Tiles;
use Illuminate\Database\Eloquent\Model;
class Tile extends Model {
protected $table = 'tiles';
public function tags() {
return $this->belongsTo('Tiles\TileTag');
}
}
During my foreach loop it returns the tile_name and any other columns from my table, except the ones joined by the relatipnship.
#foreach($tiles as $tile)
<a href="tile/{{$tile->tile_name}}">
<li class="col-md-4 mix" data-category="{{ $tile->tags->tag_title }}">
{!! HTML::image($tile->tile_thumb, null, array('class' => 'img-responsive')) !!}
</li>
</a>
#endforeach
How can I get my categories/tags linked to my primary entries when they are sorted during each loop?
I try returning the data during the loop by {{ $tile->tags->tag_title }} but it returns an empty string.
Controller Method:
class TileController extends Controller {
/**
* Display a listing of tiles from the database.
*
* #return Response
*/
public function index() {
// Get tile data from the model
$tiles = \Tiles\Tile::all();
return view('pages.index', compact('tiles'));
}
Returned Array:
I think that you don't have to create a model for Tile_Tag. Laravel can handle ManyToMany relationships out of the box (I suppose that this is the type of the relationship because you use pivot table). Your models should be
class Tile extends Model {
protected $table = 'tiles';
public function tags() {
return $this->belongsToMany('Tiles\Tag');
}
}
and
class Tag extends Model {
protected $table = 'tags';
public function tiles() {
return $this->belongsToMany('Tiles\Tile');
}
}
Laravel will know that you have a pivot table named "tag_tile" with columns "tag_id" and "tile_id". Check the relevant documentation here
Then you can iterate through tags collection for each tile like this
#foreach ($tiles as $tile)
{!!$tile->tile_name!!}
#foreach ($tile->tag as $tag)
{!!$tag->tag_title!!}
#endforeach
#endforeach
Hope it helps.