mercredi 29 avril 2020

How to create a route to show Laravel API relationships?

I am able to use Tinker to see relationship between product and reviews. Results are returned.

php artisan tinker
>> App\Model\Product::find(1)->reviews

How do I create a route and a controller to show this relationship because the show method is already being used to show by ID. Do I use the product controller or the reviews controller? Can I show both ways reviews by product and products by reviews?

UPDATED

Here is my products data

Data: [
{
productName: "television",
price: null,
id: 1
},

Here is my reviews data

data: [
{
id: 2,
customerId: "4",
booktitle: null,
description: null,
likes: null,
customer: null,
body: null,
star: null,
productId: "1"
},

Here is my review model

<?php

namespace App;

use App\Product;
use Illuminate\Database\Eloquent\Model;

class Review extends Model
{
    public $timestamps = true;

    protected $table = 'REVIEWS';

    protected $fillable = [
    'booktitle', 'description', 'updated_at', 'created_at',
    ];

/*  public function customer()
    {
        return $this->hasMany(Customers::class); 
    }*/

    public function product()
    {
        return $this->hasMany(Product::class); 
    }

}

I know I don't have a review field in the product table.

This route http://localhost:8000/api/v1/reviews/2/products is trying to run a query select * from [PRODUCTS] where [PRODUCTS].[review_id] = 2 but review_id is not a column name in my table so I don't know where this is being picked up from.

This route http://localhost:8000/api/v1/products/2/reviews is returning a 200 ok but is a blank screen.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2SlEFTA
via IFTTT

Aucun commentaire:

Enregistrer un commentaire