Reputation: 71
Hello guys i want to echo my product title but am getting Property [title] does not exist on this collection instance error message using
{{$order->produ->title}}
This is what i have in my blade file
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Customer Address :<span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-7 col-xs-12" name="address" value="{{$order->customer_address}}">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Customer City :<span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-7 col-xs-12" name="city" value="{{$order->customer_city}}">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Payment Amount:<span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-7 col-xs-12" name="total" value="{{$order->pay_amount}}">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Product Orderded:<span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-7 col-xs-12" name="total" value="{{$order->produ->title}}">
</div>
</div>
This is my product model https://i.postimg.cc/fT4t8xFv/products.png
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'title', 'category', 'tags', 'description', 'sizes', 'price', 'previous_price', 'stock',
'feature_image', 'policy', 'featured', 'views', 'created_at', 'updated_at', 'status'
];
public static $withoutAppends = false;
public function vendor()
{
return $this->belongsTo('App\Vendors', 'vendorid', 'id');
}
public function order()
{
return $this->belongsTo('App\Order', 'products');
}
}
This is my order model https://i.postimg.cc/yN5YkKCT/orders.png
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected $fillable = [
'customerid', 'products', 'quantities', 'method', 'shipping', 'pickup_location', 'pay_amount',
'txnid', 'charge_id', 'order_number', 'payment_status', 'customer_email', 'customer_name',
'customer_phone', 'customer_address', 'customer_city', 'customer_zip', 'shipping_name',
'shipping_email', 'shipping_phone', 'shipping_address', 'shipping_city', 'shipping_zip',
'order_note', 'booking_date', 'status'
];
public $timestamps = false;
public static $withoutAppends = false;
public function produ()
{
return $this->hasMany('App\Product', 'orderid', 'id');
}
}
ERROR = Property [title] does not exist on this collection instance
Upvotes: 0
Views: 188
Reputation: 14278
The error is self explanatory. It says that you are trying to get a property from the collection instance instead of the Product
item, which means you need to iterate over the products and print out its title. So you can use it like this:
@foreach($order->produ as $product)
<li>{{ $product->title }}</li>
@endforeach
Upvotes: 1