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

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

Related

Not getting all entries of an attribute in Laravel

In my controller I've this method:
public function code($code)
{
$user = Auth::user();
$instituteCodes = DB::table('institute_codes')->where(['code' => $code, 'institute_id' => Auth::id()]);
if($instituteCodes->exists()){
$claim = DB::table('code_claims')->where('institute_code_id', $instituteCodes->value('id'))->get();
$allusers = collect($claim);
$allusers->values()->all();
$course = Course::findOrFail($instituteCodes->value('course_id'));
return view('institute.code', compact(['allusers', 'course']));
}
}
institute_codes table
id
code
course_id
1
ABC
1
2
ZXC
5
course table
id
name
1
Python
2
Laravel
I've a route which passes $code parameter to public function code() I'm getting same course data on using different codes in blade view but when I use return $course or dd($course) in controller it returns the the expected result i.e. different courses for different codes. But in blade I'm getting same courses. Any help will be appreciated.
This is happening!
Why both codes are returning same course in blade and different courses(this is what I want) on using dd or return.
Edit 1
Blade View
<div class="table-wrapper">
<table class="fl-table">
<thead>
<tr>
<th class="long-table-row">Email</th>
<th>Code</th>
<th>Course</th>
<th>Language</th>
<th>Enrolled On</th>
<th>Progress</th>
<th>Certificate</th>
<th>Action</th>
</tr>
</thead>
#foreach ($allusers as $item)
<tr>
<td class="long-table-row">
{{ $item->user_email }}
</td>
<td class="uppercase">{{ $code }}</td>
<td>{{ $course->c_name }}</td>
<td>{{ $course->language->name }}</td>
<td>29/07/2022</td>
<td>100%</td>
<td><i class="bi bi-check-circle-fill" style="font-size: 1.2vw; color: rgb(0, 200, 90);"></i></td>
<td></td>
</tr>
#endforeach
<tbody>
</table>
</div>
I'm getting same c_name and language name for different codes.
I think you are iterating through the allusers but for course you are only picking the first course in the foreach loop of allusers.

search for data in a table in laravel

im trying to search for name or id of students in the table but i do not know how to do that with router and controller. I already have the table and search form with button i need the list to show all data initially then display only the entered data when a name is searched
here is my search form
<!-- Search form -->
<form class="d-flex align-items-center flex-nowrap" action="/search">
<input name="q" class="form-control" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-sm btn-info">Search</button>
</form>
and this is the table
<table class="table">
<thead class="thead-light">
<tr>
<th scope="col">id</th>
<th scope="col">First name</th>
</tr>
</thead>
<tbody>
#foreach($students as $student)
<tr>
<td>{{ $student->cne }}</td>
<td>{{ $student->firstName }}</td>
<td>
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
I would love the realtime search thingy so I can get rid of the search button lol
thank you
This is how I would do it (without the realtime thingy, check my comment for that)
In your controller that returns your students, you can grab the request and if it has a q parameter then you can filter your query.
public function index(Request $request)
{
$students = Student::query();
if($request->has("q") {
$students->where("cne", $request->get("q"))
->orWhere("firstName", $request->get("q")
}
return view("students.index", [
"students" => $students->get();
]);
}
You can use a LIKE operator if you'd prefer.

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>

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

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