Populated Array Throws Undefined Offset: 1 In Laravel 5.6 - laravel-5.6

I have a laravel 5.6 query that populates an array with a single result to pass to a view.
This is the query :
$farms=DB::select("select pic,shop_name,StateName,amount,
duration,ror,farm_shop.id from farm_shop join states on states.id=farm_shop.state_id where
farm_shop.id=:id",['id' => $id]);
A print_r command returns this:
Array ( [0] => stdClass Object ( [pic] =>
092720180421565bac5ae4974f9MAIN.jpg
[shop_name] => Pig Farm [StateName] => STATEGTR
[amount] => 3000000 [duration] => 11 [ror] => 28 [id] => 4 ) )
My view accesses it like this :
#foreach($farms->as $farmshop)
<div class="row course-set courses__row col-md-6 col-offset-md-3">
#php
$pic="http://xxxxxx/storage/".$farmshop['pic'];
#endphp
<section class="col-md-3">
<img src="{{$pic}}" alt="" width="1200" height="1200" title="xxx">
<div class="text-center"><b>{{$farmshop['shop_name']}}</b><br/>
Location: {{$farmshop[['StateName']}}
<br/>
Amount (X):<span style="color:red">
<b> {{number_format($farmshop['amount'],2)}}</b></span><br/>
Period (Months): {{$farmshop['duration']}}<br/>
Rate (%): {{$farmshop['ror']}} <div class="text-
center">
<div class="col-md-12 text-center">
Invest
</div>
<div> </div>
</div>
</section>
</div>
#endforeach
The error that now gets thrown is :
Undefined offset: 1
And the below line gets highlighted in the inbuilt laravel function compileForEach($expression):
$iteratee = trim($matches[1]);
Please how do i resolve this?..Thanks

The problem was in this line of code: #foreach($farms->as $farmshop)
Correct code is supposed to be : #foreach($farms as $farmshop)
Thanks

Related

How the display the data that nested in the array (Laravel Vue)

Sorry. I face some problem on how to display the data that nested in array.(Vue js)
Here is the return array
I would to show the data in total_billed_by_year
I tried several other attempts but nothing works.
Could please someone help me out with this?
getInfo(index){
this.popup = true;
this.inquiryForm.total_contractual=this.pageData.ppr_data[index].total_contractual;
this.inquiryForm.bil_year =this.pageData.ppr_data[index].total_billed_by_year[index].bil_year;
this.inquiryForm.bil_total_amount =this.pageData.ppr_data[index].total_billed_year.bil_total_amount;
},
<vs-popup :active.sync="popup">
<div class="vx-row mb-base">
<div class="vx-col lg:w-1/2 w-full mb-base">
<vx-card
title="Total Bill"
icon="/images/task.png"
headerClass="bg-dark pb-6"
titleColor="#fff"
subtitleColor="#fff"
>
<template slot="no-body">
<div
id=""
class="transition-all duration-500 ease-in-out p-4"
>
<div class="grid lg:grid-cols-3 grid-cols-1">
<div class="mt-5 ml-2">
<h5>Total Contractual Amount</h5>
<div class="text-lg">
<div v-if="inquiryForm.total_contractual">
<div>RM {{inquiryForm.total_contractual}} </div>
</div><div v-else>-</div></div>
</div>
<div class="mt-5 ml-2">
<h5>Bill {{inquiryForm.bil_year}} </h5>
<div class="text-lg">
<div>RM {{inquiryForm.bil_total_amount}}
</div>
</div>
</div>
</div>
</template>
</vx-card>
</div>
</div>
</vs-popup>
<vs-list-item
class="hover:shadow"
v-for="(tr, index) in ppr"
v-bind:key="index"
>
<template slot="subtitle">
<div #click="getInfo(index)" class="cursor-pointer">{{tr.id}}</div>
</template>{{tr.total_billed_by_year[index].bil_year}}
<span class="font-bold truncate overflow-auto">{{tr.month}} -{{tr.year}}
<p v-for="(bill,ind) in tr.total_billed_by_year" v-bind:key="ind">
{{bill.bil_year}}{{bill.bil_total_amount}}
</p>
</span>
</vs-list-item>
I think the easiest way would be make your column total_bil_year to return as a array via casting from its model. Something like below (if total_billed_by_year column is a json column)
protected $casts = [
'total_billed_by_year' => 'array'
];
Based on the image you attached to the question shows that it is returned as a json. So you can convert it as object at vue also. Something like below
JSON.parse(tr.total_billed_by_year) which will convert into a array and that array contain the object. See the below image. I just reproduced it at console
you can use it like below. Or just make a function to convert your jsons to objects where you can use it when you want
</template>{{JSON.parse(tr.total_billed_by_year)[0].bil_year}}
baby, using JSON.parse(yourJSONString) to transform your json string to json object.

Paginate don't fill first page? - Laravel 6

I am using the page of Laravel with order by desc and I indicate that 5 elements are displayed.
When returning 7 the first page shows me 2 and the second 5. I want the first one to be filled before the second. If I use ASC it works correctly.
Controller:
public function list(){
$questions = Question::orderBy('id', 'DESC')->paginate(5);
return view('web.pages.list', compact('questions'));
}
Vista:
#foreach ($questions as $q)
<a href="/list/{{$q->category($q->category_id)}}/{{$q->id}}">
<div class="container-list-question">
<div class="header-category-list-question">
<span>{{$q->category($q->category_id)}}</span>
</div>
<div class="data-question">
Autor: {{$q->author}}
</div>
<div class="title-list-question">
<span>{{$q->question}}</span>
<div class="btn-show"> Show question</div>
</div>
</div>
</a>
#endforeach
{{ $questions->links() }}

Laravel custom validation message for multiple fields with the same name

I have the following validation in the controller's action:
foreach ($request['qtys'] as $key => $val){
if (!$this->_validateMinQty($key, $job, $val)){
$customerTitle = $job->customers()->where('customer_id',$key)->first()->title;
return redirect()->back()->withErrors(['qtys' => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
}
}
This check multiple form's input named qtys in the view:
#foreach($job->customers as $customer)
<div class="form-group {{$errors->first('qtys has-error')}}">
{!! Form::label('qtys-'.$customer->id, __('Qty').' '.$customer->title) !!}
<div class="row">
<div class="col-md-9">
{!! Form::text('qtys['.$customer->id.']',$customer->pivot->e_production,['class' =>'form-control qtys', "data-sumequal"=>"qty",'required' => 'required','title' => $customer->pivot->aid,'id' => 'qtys-'.$customer->id]) !!}
<div class="help-block with-errors"></div>
#php ($eleE = $errors->first('qtys'))
#include('layouts.form-ele-error')
</div>
<div class="col-md-3">
<i class="fox-add"></i>{{__('Add Storage')}}
</div>
</div>
</div>
#endforeach
The above code works, but with the following limitation:
The error message is rendered under every input named qtys[x] where x is an integer and the first input only Testana has the invalid qty, like the following screen shot:
In the controller's action return message, I have tried to use indexed name for the input like the following:
return redirect()->back()->withErrors(['qtys.10' => ....
However, it prevents rendering the error message under any qtys field. Is there any solution?
The solution that I have found starts from the definition of first method found in the view :
#php ($eleE = $errors->first('qtys'))
This, in my code, should be changed to:
#php ($eleE = $errors->first('qtys.'.$customer->id))
Because the multiple fields have gotten keys equals to the customer id. This is a technique I usually use, when I want to send double piece of data in single post or in single form element.
Then in the controller, I keep the first try,
return redirect()->back()->withErrors(['qtys.'.$key => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
Where $key is an integer.

Symfony DomCrawler can't get attribute

For this div:
<section class='menu'>
<div class="some beautiful classes" link="/some/path/in/web">
<div class="contImgHome">
<img src="/images/icon.jpg alt="">
</div>
<h5>
Go to Section
</h5>
</div>
<div class="some beautiful classes" link="/another/path/in/web">
<div class="contImgHome">
<img src="/images/icon.jpg alt="">
</div>
<h5>
Go to another section
</h5>
</div>
</section>
I make:
$response = $guzzleClient->request('GET', $url_base);
$crawler = new Crawler( (string) $response->getBody() );
$crawler->filter('section.menu > div')->each(function( Crawler $div, $i )
{
$xx = $div->extract( [ 'class', 'link'] );
print_r( $xx );
echo PHP_EOL;
die;
});
And return this:
Array
(
[0] => Array
(
[0] => some beautiful classes
[1] =>
)
)
I also tried with:
$div->attr('classes');
$div->attr('link');
But 'link' attribute is always empty. ¿Why I cant get "link" attribute?
I'm using Laravel 5.2 and installed Symfony Crawler vía composer.
It was my mistake!
Web set that "link" attribute with javascript using Document Ready, so attr is not existing when Crawler gets page DOM.

Illegal string offset Laravel 5.3

I'm trying to build a 'breadcrumb' thing dynamically using an array and this pass it to the view
In my controller
//build the breadcrumbs
$breadcrumbs = [
[
'link' => 'test link',
'name' => ''
]
];
return view('home',compact('breadcrumbs'));
the 'home.blade.php' extends the 'master.blade.php' where the
#include('_breadcrumbs')
resides. And here's the contents of '_breadcrumbs.blade.php'
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<ol class="breadcrumb">
<li #if(!isset($breadcrumbs))class="active"#endif>
<i class="fa fa-dashboard"></i> Dashboard
</li>
#if(isset($breadcrumbs))
#foreach($breadcrumbs as $b)
<li>{{$b['name']}}</li>
#endforeach
#endif
</ol>
</div>
</div>
<!-- /.row -->
but it gives me this error
Illegal string offset 'link' (View:
C:\laravel-projects\dinhi_ecommerce\resources\views_breadcrumbs.blade.php)
(View:
C:\laravel-projects\dinhi_ecommerce\resources\views_breadcrumbs.blade.php)\
If I do
dd(var_dump($breadcrumbs));
it gives me the contents of the array 'breadcrumbs' which means it does work in the controller part.
Any ideas, help please?
UPDATE:
I actually have a line where I modify the index 'name' in the array which I forgot to include in my post. Anyway I change it from
$breadcrumbs['name'] = categories::where('cat_id',$id)->first()->cat_name;
to
$breadcrumbs[0]['name'] = categories::where('cat_id',$id)->first()->cat_name;
and now its working :)

Resources