0

Product name/title table column not eching with My eloquent relationship used in the code

@if(is_null($order->orderid)) No Product @else {{$order->products- >title}} @endif

    <tbody>
   @foreach($orders as $order)                     
       <tr class="">               
        @endif
         <td>{{$order->customer_email}}</td>
         <td>{{$order->customer_name}}</td>
         <td>{{array_sum($order->quantities)}}</td>
         <td>{{$order->customer_city}}</td>
         <td>@if(is_null($order->orderid)) No Product @else {{$order->products->title}} @endif</td>

         <td>{{$order->method}}</td>
        </tr>
   @endforeach
</tbody>

This is my product model and ![database structure]https://i.postimg.cc/fT4t8xFv/products.png [![product model screenshot][1]][1]

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 and ![database structure]https://i.postimg.cc/yN5YkKCT/orders.png [![order model screenshot][2]][2]


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 products()
    {
        return $this->hasMany('App\Product', 'orderid', 'id');
    }
}

Trying to get property of non-object

DoubleAsam
  • 71
  • 1
  • 7
  • Add foreign key of `orders` table to `products` table. – Zeshan Jul 28 '19 at 05:48
  • 1
    you can't access directly `$order->products->title` it's in hasMany relationship. `public function order() { return $this->belongsTo('App\Order', 'here should be foreign key not primary key', 'id'); }` Or in relationship you mention order related to many products but in screenshot of product table there is no product id. I think you misunderstood relationship. – Dilip Hirapara Jul 28 '19 at 05:49
  • You are messing up the relationship. If you want to use hasMany relationship use a pivot table or make a new row for every product in a order and use belongsTo relationship. – zahid hasan emon Jul 28 '19 at 06:14
  • Already added the order_id foreign key – DoubleAsam Jul 31 '19 at 23:16

1 Answers1

0

Your products() is a collection of objects and you can't get title of that, if you want to show products of the order, I suggest this way:

<td>
    @if(is_null($order->products)) 
    No Product 
    @else
        @foreach($order->products()->get() as $product)
             {{$product->title}} - 
        @endforeach
    @endif
</td>
Hamid Haghdoost
  • 740
  • 1
  • 6
  • 15