So i have this eloquent query
$categories = categories::all();
foreach($categories as $c):
$c->items = item_categories::where('cat_id',$c->cat_id)->with('item')->with('item.item_images')->get();
endforeach;
and in my blade, I can display this part
with('item')
like doing
#foreach($categories as $c)
#foreach($c->items as $i)
{{ $i->item->item_name }}
#endforeach
#endforeach
How about retrieving data from this part?
with('item.item_images')
I tried
#foreach($categories as $c)
#foreach($c->items as $i)
{{ $i->item->item->item_images->image_name }}
#endforeach
#endforeach
but it gives me error
Trying to get property of non-object
Any ideas, help please?
here's the 'categories' model
class categories extends Model
{
protected $table = 'categories';
protected $primaryKey = 'cat_id';
public function item_category(){
return $this->belongsTo('App\item_categories','cat_id','cat_id');
}
}
and the 'item_categories' model
class item_categories extends Model{
protected $table = "item_categories";
public function item(){
return $this->belongsTo('App\items','item_id','item_id');
}
public function category(){
return $this->hasOne('App\categories','cat_id','cat_id');
}
}
and the 'items' model
class items extends Model
{
protected $table = 'items';
protected $primaryKey = 'item_id';
public function item_images(){
return $this->hasMany('App\item_images','item_id','item_id');
}
public function item_categories(){
return $this->hasMany('App\item_categories','item_id','item_id');
}
}
You're going too deep. It looks like item_categories has an item, which has an item_image, so you just need to access it like this:
{{ $i->item->item_images->image_name }}
Since each item has many item_images, you need to iterate through it.
#foreach($i->item->item_images as $image)
{{$image->image_name}}
#endforeach
My guess is that your code is okay but item doesn't have item_image. Try checking if checking if $i->item->item->item_images is empty before trying to use the value
Related
I am have some issue with eloquent relationship my first time working with it, after selecting and displaying list of selected events, I want to also select the ticket_id base on the event_id common between both(events and events_ticket) table, thanks for the help
Error Showing ->
Trying to get property 'ticket_id' of non-object (View: C:\wamp64\www\mahive\resources\views\tickets\index.blade.php)
Both Model
class EventsTicket extends Model
{
public $table = "events_ticket";
use HasFactory;
protected $fillable = [
'event_id',
'ticket_id',
'ticket_name',
'ticket_amount',
'ticket_sold',
];
public function event() {
return $this->belongsTo('App\Models\Event');
}
}
class Event extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'event_id',
'event_name',
'event_category',
'event_type',
'event_mode',
'event_description',
'event_image'
];
public function userModel() {
return $this->belongsTo('App\Models\User');
}
public function eticket() {
return $this->hasMany('App\Models\EventsTicket');
}
}
Controller
public function index()
{
$events = Event::where('ticket_statue', 'active')->with('eticket')->get();
return view('tickets.index', ['event_data' => $events]);
}
View
#foreach($event_data as $event)
{{ ucwords($event->event_name) }}
{{ $event->eticket->ticket_id }}
#endforeach
In this code every Event has many eticket. Try this:
#foreach($event_data as $event)
{{ ucwords($event->event_name) }}
{{ optional($event->eticket->first())->ticket_id }}
#endforeach
Use optional because there may not be any etickets.
I have a Recipe Model and FoodComponent Model which belongsTo Recipe.
RecipeModel :
class Recipe extends Model
{
protected $table = 'recipes';
protected $fillable = [
'details'
];
public function foods(){
return $this->hasMany('App\FoodComponent','id','food_id');
}
}
FoodComponent model:
class FoodComponent extends Model
{
protected $table = 'food_components';
protected $fillable = [ 'title',
'menu_id',
'image'
];
public function recipes(){
return $this->belongsTo('App\Recipe','id','food_id');
}
}
RecipeController:
public function index()
{
$recipes = Recipe::all();
return view('recipes.index')->with('recipes',$recipes);
}
And in the view I have
#foreach($recipes as $recipe)
{{ $recipe->foods }}
#endforeach
I get something like this :
[{"id":1,"menu_id":1,"title":"Pollo Locco","image":"images\/uploads\/foodcomponents\/99254.jpg","created_at":"2016-08-12 10:01:38","updated_at":"2016-08-12 10:01:38"}]
But I want just the 'title'.
I've tried with $recipe->foods->title. But is not working.
Can you give me an idea how can I display just the title please ?
Thank you !
Try this
#foreach($recipes as $recipe)
#foreach($recipe->foods as $food)
{{ $food->title }}
#endforeach
#endforeach
I'm trying to print all books' name in 'books' table and its category associated. But it doesn't show any content nor error messages.
Model::Book.php
class Book extends Eloquent{
protected $table ='books';
public function bookCat()
{
return $this->belongsTo('BookCategory');
}
}
Model::BookCategory.php
class BookCategory extends Eloquent{
protected $table ='book_categories';
}
Controller::route.php
Route::get('/', function()
{
$books = Book::all();
return View::make('books')
->with('books', $books);
});
View::books.blade.php
#foreach ($books as $book)
<li>{{ $book->book_name }}</li>
- <small>{{ $book->bookCat }}</small>
#endforeach
Table::books
(int)id, (string)book_name, (int)category_id
Table::book_categories
(int)id, (string)category
Add the relationship to BookCategory.
class BookCategory extends Eloquent{
protected $table ='book_categories';
public function books() {
return $this->hasMany('Book');
}
}
Your local key doesn't match the model names, either, so you would need to specify that in the relation:
class Book extends Eloquent{
protected $table ='books';
public function bookCat()
{
return $this->belongsTo('BookCategory', 'category_id');
}
}
You may also want to eager-load the categories in your controller, as your query is for the books:
$books = Book::with('bookCat')->get();
I have three models:
class Brand extends \Eloquent {
protected $fillable = [];
public function product()
{
return $this->hasMany('Product');
}
}
class Product extends \Eloquent {
protected $fillable = [];
public function reviews()
{
return $this->hasMany('Review');
}
public function brand()
{
return $this->belongsTo('Brand');
}
}
class Review extends \Eloquent {
protected $fillable = [];
public function product()
{
return $this->belongsTo('Product');
}
}
I'm trying to display brand names along with product and review counts in a view:
{{ $brand->product->count() }}
it works for this, but doesn't display reviews count:
{{ $brand->product->reviews->count() }}
neither for:
{{ $brand->product->reviews->count() }}
Errors I'm getting are:
ErrorException (E_UNKNOWN)
Undefined property: Illuminate\Database\Eloquent\Collection::$review
ErrorException (E_UNKNOWN)
Undefined property: Illuminate\Database\Eloquent\Collection::$reviews
The problem is that you can't call a relation on the collection of a model but only on the model itself. This means you have to loop over the products and count the reviews of each one of them.
Basically like that
$counter = 0;
foreach($brand->product as $product){
$counter += $product->reviews()->count();
}
echo $counter.' reviews!';
Now this is very bad on database performance. First it queries the products and for each product it makes another request to the db. We can use eager loading to avoid this.
$counter = 0;
$products = $brand->product()->with('reviews')->get();
foreach($products as $product){
$counter += $product->reviews()->count();
}
echo $counter.' reviews!';
With eager loading it loads all the data with one query and its already in memory when we do $product->reviews()
To finish things off here we can now put this in a function in the brand model
public function getProductReviewCount(){
$counter = 0;
$products = $this->product()->with('reviews')->get();
foreach($products as $product){
$counter += $product->reviews()->count();
}
return $counter;
}
{{ $brand->getProductReviewCount() }}
Sidenote: I would also suggest you change the name of the relationship product into products. It makes more sense and is usually convention to use the plural.
I am using
{{ count($brand->product) }}
{{ count($brand->product->reviews) }}
I have 3 tables: Users, Projects, Items and Friends. I want to get all the items for a project and list each item with related friends. Am I missing something in my model? Ultimately I want to get all the friends which are related to the items.
// CONTROLLER
public function show($id)
{
$uid = Auth::user()->id;
$projects = Project::find($id)->with('item.friend')->get();
return View::make('projects.show', compact('projects'));
}
//VIEW
#foreach ($projects as $project)
#foreach ($project->friend as $friend)
<li>
<a href="#" class='itemLink' >{{$friend->email}}</a>
<a href="#" class='itemLink' >{{$projects->item->name}}</a>
</li>
#endforeach
#endforeach
// MODELS
class User extends Eloquent {
public function project()
{
return $this->hasMany('Project');
}
public function item()
{
return $this->hasMany('Item');
}
public function friend()
{
return $this->hasMany('Friend');
}
class Project extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function item()
{
return $this->hasMany('Item');
}
public function friend()
{
return $this->hasMany('Friend');
}
class Item extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function friend()
{
return $this->hasMany('Friend');
}
class Friend extends Eloquent {
protected $guarded = array();
public static $rules = array();
You are missing a loop. I recommend that when setting up a many to many relationship, make sure your method is plural. It makes a lot more sense when trying to read the code. You'd then send up with #foreach $project->items as $item or $item->friends as $friend.
#foreach ($projects as $project)
#foreach ($project->item as $item)
#foreach($item->friend as $friend)
<li>
<a href="#" class='itemLink' >{{$friend->email}}</a>
<a href="#" class='itemLink' >{{$item->name}}</a>
</li>
#endforeach
#endforeach
#endforeach
Your models seems OK at first look. I think since you are pulling item.friend relationship, this line
#foreach ($project->friend as $friend)
should be,
#foreach ($project->item->friend as $friend)