How to pass variable from Guzzle Request In Laravel - php

I'm trying to pass a variable from guzzle request to my view.
This is my controller
$client = new Client();
$res = $client->request('GET', 'https://api.iugu.com/v1/customers?api_token=secret');
$result = $res->getBody();
$clientes = json_decode($result, true);
return view('sections.client.index')->with('clients', $clientes['items']);
But return error:
Trying to get property of non-object (View: /var/www/html/cron_verify/resources/views/sections/client/index.blade.php)
This is my JSON
And this is my view
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12 col-md-3-offset table_box">
<table class="table">
<tr>
<th>Nome</th>
<th>Status</th>
<th>Faturas</th>
<th>Situação</th>
</tr>
#foreach($clients as $value)
<tr>
<td>{{$value->name}}</td>
<td>{{$value->email}}</td>
<td>{{$value->number}}</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
#endsection
I dont understand why I am getting this error because the value is in the JSON response. What is causing the error?

you need to access element as array value.
#foreach($clients as $value)
<tr>
<td>{{$value["name"]}}</td>
<td>{{$value["email"]}}</td>
<td>{{$value["number"]}}</td>
</tr>
#endforeach

As the error says, you are working with arrays, no with objects. Use this foreach instead.
#foreach($clients as $value)
<tr>
<td>{{$value['name']}}</td>
<td>{{$value['email']}</td>
<td>{{$value['number']}}</td>
</tr>
#endforeach

You are accessing the array wrongly
Try:
#foreach($clients as $value)
<tr>
<td> {{$value["name"]}} </td>
<td> {{$value["email"]}} </td>
<td> {{$value["number"]}} </td>
</tr>
#endforeach

It's happend becasue you are using $clientes = json_decode($result, true); returning an associative object, than, on your view you must use this:
<tr>
<td>{{$value['name']}}</td>
<td>{{$value['email']}}</td>
<td>{{$value['number']}}</td>
</tr>

Related

How to Get Count of variable in JSON Response Laravel 8

I have this json response:
How to get count of "members", i tried this in view:
<tbody>
#foreach ($datas as $post)
<tr
<td scope="row">{{ sizeof($post ->json-> members) }}</td>d>
</tr>
#endforeach
</tbody>
But i got this error:
ErrorException
sizeof(): Parameter must be an array or an object that implements Countable (View: C:\xampp\htdocs\firstapp\resources\views\s1.blade.php)
Any Kind of Help Please?
I found the Solution, i use this in view:
#foreach ($datas as $post)
<tr>
<td scope="row">{{ count((explode(',', json_encode($post->json->members)))) }</td>
</tr>
#endforeach
count(json_decode($post[0]->json->members, true));
My bad. Sorry;
Here solution
count(json_decode(json_encode($post[0]->json->members), true));

Pagination in laravel returning method undefined

The pagination method returns App\Article::link undefined, any ideas? I thought this was a helper method
public function index()
{
$data = Article::paginate(5);
return view('datatypes.articles.index', compact('data'));
}
in my view:
<tbody>
#foreach($data as $data)
<tr>
<th scope="row">{{$data->id}}</th>
<td>{{$data->title}}</td>
</tr>
#endforeach
</tbody>
{{ $data->links() }}
<tbody>
#foreach($data as $_data)
<tr>
<th scope="row">{{$_data->id}}</th>
<td>{{$_data->title}}</td>
</tr>
#endforeach
</tbody>
{{ $_data->links() }}
try this in your view
<tbody>
#foreach($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->title}}</td>
</tr>
#endforeach
</tbody>
{{ $data->links() }}

Property [id] does not exist on this collection instance laravel

I keep getting this error after fixing a new code that I had just put in and I have no idea why it is giving me this error "Property [id] does not exist on this collection instance" which point to the id of my route in home.blade.php. Can someone help me take a look thanks a lot
The part that I added in was the hire_status part and after fixing it, it then gave me this error.
home.blade.php
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Hire Status: </big></strong></th>
<th><strong><big>Action: </big></strong></th>
</tr>
<td>
<tr>
#foreach($data as $value)
<tr>
<th>{{$value->Name}}</th>
<th>
#foreach($data1 as $value1)
{{$value1->hire_status}}
</th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="get">
{{ csrf_field() }}
<button type="submit">Delete</button>
</form></th>
</tr>
#endforeach
#endforeach
</tr>
</tr>
</table>
HomeController:
public function getData(){
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
$data1 = DB::table('hires')->where('hire_status','Yes')->get();
if(count($data)>0){
return view('home',compact('data','data1'));
}else{
return view('home');
}
}
Just remove key from array,
you are trying to get data in your view directly so get it directly not to data of data...
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()‌​->sortByDesc('create‌​d_at');
change it to,
$data = DB::table('personal_infos')->where('deleted_at',NULL)->get()‌​->sortByDesc('create‌​d_at');

Trying to get property of non-object Laravel 5.4

Hi Guy's Im always getting an error from laravel 5.4.
Im creating a list of members... but when i use #foreach it says
ErrorException in 6163a8b030c7474bc8eaad359ab99eb61ebdb127.php line 38:
Trying to get property of non-object (View: E:\wamp64\www\gplspring2017\resources\views\admin\memberlist.blade.php)
Here's my controller
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->first();
$count = count($teams);
if (!$count) {
return redirect('404');
} else {
return view('/admin/memberlist', ['team' => $teams]);
}
}
and here's my view code:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($qmember as $get_member)
<tr>
<td><img src="{{ $get_member->member_pic }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>
When i remove the foreach the code is working.. but i try to add the #foreach($qmember as $get_member) it not working anymore...
Check this line:
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
when you are using first() then it does not return an Std Class object. If you want it so then use get()
Using first() method you are fetching only one record from DB that match with the ID (First one matched). You need to use get().
Controller:
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->get(); //Use get() if you expect more than one result not first()
$count = count($teams);
if(! $count)
{
return redirect('404');
}
else
{
return view('/admin/memberlist', ['teams' => $teams]);// this team variable must be used in the blade
}
}
View:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($teams as $team)
<tr>
<td><img src="{{ $team['member_pic'] }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>

Cant pass value from Controller to .blade.php

I am trying display all product name,
I am getting the error :
ErrorException in 4938c589ea3db6bab0c4c788473314a4 line 46:
Undefined variable: allproduct (View: C:\Server\WebDocs\CRUD\resources\views\product.blade.php)
I had try return View::make('product',$allproduct); and
return View::make('product')->with('product',allproduct); also cannot, i don't know why, this is my first Laravel program.
in my ProductController
public function index()
{
// get all the Products
$allproduct = Product::all();
return View::make('product',$allproduct);
}
in my product.blade.php
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>No</td>
<td>Product</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
#foreach($allproduct as $key => $value)
<tr>
<td>$key</td>
<td>{{ $value->product }}</td>
<!-- we will also add show, edit, and delete buttons -->
<td>
</td>
</tr>
#endforeach
</tbody>
</table>
You could try this,
return View::make('product', compact('allproduct'));
Trying using this code in Controller:
return View::make('product')->with(['allproduct' => $allproduct]);
try passing variable like
public function index()
{
// get all the Products
$allproduct = Product::all();
return View::make('product')->with('allproduct', $allproduct);
}
and in view
what about just doing #foreach($allproduct as $product) and then in <td>, <td>{{$product->product}}</td>. and make sure your have product column in db
controller:
return View::make('product')->with('allproduct',$allproduct);
blade:
#foreach($allproduct as $key => $value)
<div>
{{ $key . ' => ' . $value }}
</div>
#endforeach

Categories