उत्पाद में एक-से-कई संबंध हैं, 'उत्पादब्रांड', टेबल उत्पादब्रांड और उत्पादब्रांड के साथ टेबल ब्रांड के साथ एक-से-एक संबंध, 'ब्रांड' है। टेबल ब्रांड में एक कॉलम, 'ब्रांड' है। और मैं उत्पाद के ब्रांड तक पहुंचने में सक्षम हूं। अन्य सभी श्रेणियों, उपयोगकर्ता नाम, आदि का उपयोग ठीक से किया जाता है।जावास्क्रिप्ट कोड में लार्वा संबंधों तक पहुंच
public function show(Request $request)
{
if($request->ajax())
{
$id = $request->id;
if($id)
{
$show = Product::where(['product_id'=>$id])->first();
$category = $show->category->category;
$username = $show->user->username;
$getbrands = $show->productbrand;
foreach($getbrands as $getbrand)
{
$brand=$getbrand->brand->brand;
}
if($show)
{
echo json_encode(array('status' => TRUE, 'show' => $show, 'username' => $username, 'category' => $category, 'brand' => $brand)); die;
}
}
}
echo json_encode(FALSE);die;
}
अजाक्स और jQuery:
$.ajax({
type: "POST",
url: "{{url('/product/show')}}",
data: {id:id},
success: function (data) {
var res = $.parseJSON(data);
if(res.status == true)
{
var result = 'Category: ' + res.category + '<br>' +
'Product by: ' + res.username + '<br>' +
'Brands: ' + res.brand + '<br>' +
'Price: ' + res.show.price + '<br>' +
'Price type: ' + res.show.price_type + '<br>' +
'Product Views: ' + res.show.views + '<br>';
$('#result').html(result);
}
}
});
इस तरह से मैं केवल एक ही ब्रांड मिल सकता है। मैंने निम्नलिखित तरीके से भी कोशिश की है, लेकिन असफल रहा।
नियंत्रक में:
$getbrands = $show->productbrand;
echo json_encode(array('status' => TRUE, 'show' => $show, 'username' => $username, 'category' => $category, 'getbrands' => $getbrands));
अजाक्स में:
for(var i=0; i<res.getbrands.length; i++)
{
var brands=res.getbrands[i].brand.brand; //First 'brand' is relation and second is the brand I am trying to access
}
आज़माएं: $ शो = उत्पाद :: जहां ([ 'product_id' => $ id]) -> ('productbrand') के साथ -> पहले(); –
@Autista_z समस्या जावास्क्रिप्ट के साथ पहुंच रही है। – micky
मैं एक उत्तर लिख रहा था, लेकिन मैंने देखा है कि आपका कोड बहुत अजीब है। मैं वास्तव में आपको www.laracasts.com सबक देखने के लिए सुझाव देता हूं, आप लार्वेल के बारे में बहुत कुछ सीखेंगे। सौभाग्य। –