How to Get Count of variable in JSON Response Laravel 8 - php

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));

Related

Call to undefined method App\UserShiftPattern::userWorkScheduleList()

I want to get name from other database table, and get this error method, which part did i missed?
public function userWorkScheduleList()
{
return $this->hasMany(UserShiftPattern::class,'id','user_id');
}
<tbody>
#foreach ($usps as $usp => $usplist)
<tr>
<td>{{ $usplist->userWorkScheduleList()->name }}</td>
</tr>
#endforeach
</tbody>

Styling table in Laravel

I have a problem with #foreach in my blade.php. When I insert movie with multiple categories my other moves to the right. Here is how it looks in browser
and here is code in blade
#if($movies)
<div class="row">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Categories</th>
<th>Actors</th>
</tr>
</thead>
<tbody>
#foreach($movies as $movie)
<tr>
<td>{{$movie->name}}</td>
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
#foreach($movie->actors as $actor)
<td>{{$actor->actor_name}}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
#endif
Your issue lies here:
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
You are saying that for each category, it should insert a new <td> into you table. This will of course skew the content since your table expects a set number of columns.
Instead, you should move your foreach statement inside the <td></td>
<td>
#foreach($movie->categories as $category)
{{$category->category_name}}
#endforeach
</td>
This will print the links for each category in the same column.

How to pass variable from Guzzle Request In Laravel

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>

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');

Call to a member function links() on array in Laravel error

I was working with Laravel and I am trying to paginate my table of books. I got this error "Call to a member function links() on array in Laravel". It may be as the duplicate's error, but I still can't figure out how to solve my thing.
BookController fiddle:
public function index()
{
$books = Book::simplePaginate(3)->all();
$authors = Author::all();
$genres = Genre::all();
return view('books.all')->withBooks($books)->withAuthors($authors)->withGenres($genres);
}
books/all.blade.php fiddle
<table class="table table-hover">
<tr class="info">
<td>#</td>
<td>Name</td>
<td>Author</td>
<td><center>Visit</center></td>
</tr>
#foreach($books as $book)
<tr>
<td width="50px"><img width="50px" height="75px" src="{{ asset('images/' . $book->image) }}"></td>
<td width="50px">{{ $book->name }}</td>
<td width="50px">{{ $book->author->name }}</td>
<td width="50px"><center><button class="btn btn-success">Visit</button></td>
</tr>
#endforeach
</table>
{{ $books->links() }}
Just remove the ->all() method from the $books variable.
$books = Book::paginate(10);
paginate() function considers taking all content from the table, which is taken here by Book model

Categories