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
>> AppModelProduct::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 AppProduct;
use IlluminateDatabaseEloquentModel;

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

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.