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
Related
Try access to show method in VehicleController() and return error: Attempt to read property 'customer' on null.
*ERROR: Property [full_name] does not exist on this collection instance
VEHICLE MODEL
class Vehicle extends Model
{
use HasFactory;
protected $table = 'vehicles';
protected $fillable = ['customer_id', 'model_name', 'brand',
'color', 'year', 'plate_number','engine_number', 'mileage', 'license_number',
'vehicle_type', 'photo_cover'
];
public function customer ()
{
return $this->hasMany(Customer::class);
}
}
CUSTOMER MODEL
class Customer extends Model
{
use HasFactory;
protected $table = 'customers';
protected $fillable = ['full_name', 'customer_type', 'email',
'phone_main', 'phone_mobile', 'doc_cpf', 'doc_cnpj', 'city', 'state',
'address', 'neighborhoods', 'number', 'zip_code'
];
public function vehicle ()
{
return $this->hasOne(Vehicle::class);
}
}
VEHICLE CONTROLLER (show)
public function show(Vehicle $vehicle)
{
$vehicle = Vehicle::select(
'vehicles.id', 'vehicles.model_name', 'vehicles.brand', 'vehicles.year', 'vehicles.color', 'vehicles.mileage',
'customers.full_name', 'vehicles.plate_number', 'vehicles.vehicle_type', 'vehicles.photo_cover'
)
->join('customers', 'customers.id', '=', 'vehicles.customer_id')
->get();
return view('admin.cars.show')->with('v', $vehicle);
}
VIEW (show.blade.php)
<?php
<td>CUSTOMER:</td>
<td class="view-row">
{{ $v->customer->full_name }}
</td>
<td>MODEL:</td>
<td class="view-row">
{{ $v->model_name }}
</td>
<td>BRAND:</td>
<td class="view-row">
{{ $v->brand }}
</td>
I don't know how to solve this, I made this change, but it didn't work. See below:
<td>CUSTOMER:</td>
<td class="view-row">
{{ $v->customer->full_name ?? 'Customer Empty' }}
</td>
VEHICLE MODEL replace this
public function customer ()
{
return $this->hasMany(Customer::class);
}
to
public function customer ()
{
return $this->belongsTo(Customer::class);
}
and VEHICLE CONTROLLER (show) replace to
$vehicle = Vehicle::with('customer')
->paginate(10);
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 am a beginner in Laravel and am stuck with sending the data from pivot table to view. I have two models one is "Supplier" and another is "Product" which I have created as follows:
Supplier.php
class Supplier extends Model
{
protected $keyType = 'string';
public $incrementing = false;
public function products()
{
return $this->belongsToMany(
'App\Product',
'inward_stock',
'supplier_id',
'product_id'
)->withPivot(
'product_dimension',
'quantity',
'expiry_date',
'type',
'QR_code',
'sales_price',
'arrival_date',
'occassion',
'discount_percentage',
'payment_date',
'cost',
'paid_status',
'sku'
)->withTimestamps();
}
}
Product.php
class Product extends Model
{
protected $keyType = 'string';
protected $primaryKey = 'product_id';
public $incrementing = false;
public function suppliers()
{
return $this->belongsToMany('App\Supplier');
}
}
Now I want to access all the data from the pivot table named as "inward_stock" from my controller and want to send it to the view named "inward-stock-list", I have created "inwardstockController" for that purpose and want to know how to send it to the view using that controller.
I think the most effective way is to create model for pivot table:
class InwardStock extends Model
{
protected $table = 'inward_stock';
public function supplier()
{
return $this->belongsTo('App\Supplier', 'supplier_id', 'supplier_id');
}
public function product()
{
return $this->belongsTo('App\Product', 'product_id', 'product_id');
}
}
inwardstockController:
$inwardStocks = InwartStock::with(['product', 'supplier'])->get();
View:
#foreach($inwardStocks as $inwardStock)
{{ $inwardStock->supplier->id }}
{{ $inwardStock->product->id }}
#endforeach
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
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();