Attempt to read property "Nombre" on bool - php

I'm trying to make a show view with Laravel 8 but i can't show the detail, this is the code from the controller:
public function show($id)
{
$accesorios=DB::table('accesorio as acc')
->join('detalle_aparato as da','acc.idAccesorio','=','da.idAccesorio')
->select('acc.Nombre')
->where('da.idAparato','=',$id);
return view("almacen.aparato.show",["accesorio"=>Accesorio::findOrFail($id)]);
}
And this is the code from the view:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
Error message
When I use:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc}}</td>
</tr>
#endforeach
It prints: 1 for each record
Hope you can help me

In you're controller you're using DB::Table to set the $accesorios variable, but never using it.
You then are setting accesorio in your view to Accesorio::findOrFail($id) which will only return one instance of the object.
Either pass $accessorios into your view
return view("almacen.aparato.show",["accesorios"=>$accessorios]);
then loop through it
#foreach ($accesorios as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
or since you're just sending one instance of the object to the view, remove the loop and you can render it like this.
<tr>
<td>{{ $accesorio->Nombre }}</td>
</tr>

Related

ArgumentCountError Too few arguments to function Laravel unable to pull data from db and display

I am working in Laravel 7, pulling data from my db and showing the student his/her score on the scores page in percentage via the following codes.
In my StudentOperationController.php
public function show_result($id) {
$data['result_info'] = Oex_result::where('id', $id)->get()->first();
$data['student_info'] = Oex_students::select(['oex_students.*', 'oex_exam_masters.title', 'oex_exam_masters.exam_date'])->join('oex_exam_masters', 'oex_students.exam', '=', 'oex_exam_masters.id')->where('oex_students.id', Session::get('id'))->get()->first();
return view('student.show_result', $data);
}
and in my show_result.blade.php
<h2>Result Information</h2>
<table class="table">
<tr>
<td>Number Correct</td>
<td>{{ $result_info->yes_ans }}</td>
</tr>
<tr>
<td>Number Incorrect</td>
<td>{{ $result_info->no_ans }}</td>
</tr>
<tr>
<td>Total</td>
<td>{{ round($result_info->yes_ans / ($result_info->yes_ans + $result_info->no_ans) * 100 , 1) }} %</td>
</tr>
</table>
The reason I show this is that it works as expected. this is within the students dashboard. In my admin, I am trying to do the same thing and show that students data for his/her exam result. In my database, I have a json type data for example: {"2":"YES","3":"YES","8":"YES"}. I am still learning Laravel and am stuck on how to get this data to my admin. I am clearly doing something wrong and am in need of assistance. Here is what I have tried:
AdminController.php
public function manage_students($id)
{
$data['result_info'] = Oex_result::where('id', $id)->get()->first();
// dd($data);
$data['student_info'] = Oex_students::select(['oex_students.*', 'oex_exam_masters.title', 'oex_exam_masters.exam_date'])->join('oex_exam_masters', 'oex_students.exam', '=', 'oex_exam_masters.id')->where('oex_students.id', Session::get('id'))->get()->first();
$data['exams'] = Oex_exam_master::where('status', '1')->get()->toArray();
$data['students'] = Oex_students::select(['oex_students.*', 'oex_exam_masters.title as exam_name'])
->join('oex_exam_masters', 'oex_students.exam', '=', 'oex_exam_masters.id')
->get()->toArray();
return view('admin.manage_students', $data);
}
and in my manage_students.blade.php, I have
<tbody>
#foreach($students as $key => $student)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $student['name'] }}</td>
<td>{{ $student['email'] }}</td>
<td>{{ $student['mobile_no'] }}</td>
<td>{{ $student['exam_name'] }}</td>
{{-- <td>N/A</td> --}}
<td>{{ round($result_info->yes_ans / ($result_info->yes_ans + $result_info->no_ans) * 100 , 1) }} % </td>
#if($student['status']== 1)
<td><input data-id="{{ $student['id'] }}" class="student_status" type="checkbox" name="status" checked></td>
#else
<td><input data-id="{{ $student['id'] }}" class="student_status" type="checkbox" name="status"></td>
#endif
<td>
Edit
Delete
</td>
</tr>
#endforeach
</tbody>
In doing this I get the error Too Few arguments to function ... 0 passed and exactly 1 expected
I am unable to replicate what I have for the student dashboard and because of my lack of Laravel knowledge, I am having trouble solving this issue. So, any help would be greatly appreciated. If I am missing anything, please let me know so I can edit my question. Thanks in advance.
Edit: manage_students route:
Route::get('admin/manage_students', 'AdminController#manage_students');
Edit Number 2:
Too few arguments to function App\Http\Controllers\AdminController::manage_students(), 0 passed and exactly 1 expected
C:\laragon\www\lionsfieldtest\app\Http\Controllers\AdminController.php:146
Referring to this line:
public function manage_students($id)
Edit Number 3:
in resources/views/layouts/app.blade.php
<li class="nav-item">
<a href="{{ url('admin/manage_students') }}" class="nav-link">
<i class="nav-icon fas fa-school"></i>
<p>
Student Management
</p>
</a>
</li>
Edit number 4:
The manage_student page brings in data from a number of student's exam results
This function request $id;
public function manage_students($id)
This route doesn't pass one
Route::get('admin/manage_students', 'AdminController#manage_students');
As I can see you are filtering by results which in this case you need $result id
Route::get('admin/manage_student/{id}', 'AdminController#manage_students');
So you have to pass result ID, but lets assume there is a default results id
public function manage_students($id=2)
then this route works because $id is optional/predefined.
Route::get('admin/manage_students', 'AdminController#manage_students');
And if you pass an ID it will use
Route::get('admin/manage_student/{id}', 'AdminController#manage_students');
Make 2 routes and make parameter optional/predefined.
if you go to /admin/manage_student it will use ID 2
but if you pass /admin/manage_student/5 it will use 5

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

i cant access my admin_table i know i have and problem on my route on the admin_table please help me on my route Property [id] does not exist on this collection instance.
my admin_table.blade
#foreach ($users as $positions)
#foreach ($positions->admins as $position )
<tbody>
<tr>
<td>{{ $position->id}}</td>
<td>{{ $position->first_name}}</td>
<td>{{ $position->last_name}}</td>
<td>{{ $position->contact}}</td>
<td>{{ $position->departments->department}}</td>
<td>{{ $position->schoolpositions->school_position}}</td>
<td>{{ $position->email}}</td>
<th> Edit
</th>
</tr>
</tbody>
#endforeach
</tr>
</tbody>
#endforeach
#endforeach
</table>
this is my route
Route::get('/admin_table', 'DashboardController#index');
Route::put('/admin_table/{id}', 'DashboardController#store');
Route::get('/admin_table', 'DashboardController#show');
Route::get('/teacher_editform/{id}', 'DashboardController#edit');
Route::put('/teacher_editform/{id}', 'DashboardController#update')->name('teacheradminpage.teacher_tableform.teacher_editform');
this is my controller
public function edit($id)
{
$departments = Department::find($id);
$users = Schoolposition::find($id);
$students = Student::find($id);
$admins = Admin::find($id);
return view('teacheradminpage.teacher_tableform.teacher_editform', compact('departments','users','students','admins', 'id', 'id', 'id', 'id'));
}
Your issue is that you put an extra $position before the array. Your route should be
Edit
You've got several issues in this code that probably need attention. The main issue with the position error, seems to be that you've got a circular path to get that $position in your anchor tab within the loop.
This section is likely not creating what you want:
#foreach ($users as $positions)
#foreach ($positions->admins as $position )
This is roughly saying - take my collection of users and make a new collection of positions that are directly attached to each user object. From here, take that collection of positions and try and find a collection of admins on that are attached to that collection and then find a $position. (I think this is close to what it is trying to do...)
When you get to the inside of the loop and your anchor / route, you are asking the anchor to find an id on something that is likely either non-existent, or a collection again.
You have already passed in admins to this view. It would make sense to use that as your top level loop item, no?
To fix, use admins as the only loop item:
#foreach ($admins as $position )
Edit Your Blade With
#foreach ($users as $positions)
#if(!empty($positions->admins)
#foreach ($positions->admins as $position )
<tbody>
<tr>
<td>{{ $position->id}}</td>
<td>{{ $position->first_name}}</td>
<td>{{ $position->last_name}}</td>
<td>{{ $position->contact}}</td>
<td>{{ $position->departments->department}}</td>
<td>{{ $position->schoolpositions->school_position}}</td>
<td>{{ $position->email}}</td>
<th> Edit
</th>
</tr>
</tbody>
#endforeach
</tr>
</tbody>
#endforeach
#endif
#endforeach
</table>

Laravel foreach multiple data

This is how I wish to be
Actual
Status gets printed 3 times
I return this objects from my controller:
return view('ViewTicket') ->with('tickets', $tickets)
->with('user', $user)
->with('priority', $priority)
->with('status', $status)
->with('type', $type);
However I want to print the respective fields like in my view:
#foreach ($tickets as $t)
<tr>
<td> {{$t->id}} </td>
#foreach ($user as $u)
#if($t->user_id==$u->Id)
<td>{{ $u->UserName }}</td>
#endif
#endforeach
Even this doesnt solve my problem.Is there any way to avoid the loop inside the loop to get these data?The goal is to get respective fields for each ticket
If I dd($user) it returns 3 values okay
when I loop in my view it displays 9 values,which means it loops 3 times the lements
Thanks in Advance
ASSUMPTIONS
$tickets is a collection of ticket objects.
$user is a collection of user objects.
CODE
#foreach ($tickets as $t)
<tr>
#foreach ($user as $u)
#if($t->user_id == $u->Id)
<td> {{$t->id}} </td>
<td>{{ $u->UserName }}</td>
#endif
#endforeach
</tr>
#endforeach

How to get data from database to view page in laravel?

I am using Laravel 5.4 and I want to view my data in database from my view page (listpetani.blade.php).
Here is the code of my project:
HTML:
<div class="table-responsive">
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th><strong>No</strong></th>
<th><strong>Nama Petani</strong></th>
<th><strong>Alamat</strong></th>
<th><strong>No. Handphone</strong></th>
<th><strong>Lokasi</strong></th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tbody>
</table>
</div>
PHP:
In my listpetani.blade.php I have an empty table and I want to show data from database tbl_user:
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
});
And the table in my page: view in browser
I want to show all the data from database into my view in laravel 5.4. Can anybody help me?
[SOLVE]
Thank you guys, I already solve this problem
This is the solved code
web.php (routes)
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->get();
return view('listpetani', ['petani' => $petani]);
});
and in my listpetani.blade.php
#foreach($petani as $key => $data)
<tr>
<th>{{$data->id_user}}</th>
<th>{{$data->nama_user}}</th>
<th>{{$data->alamat}}</th>
<th>{{$data->no_telp}}</th>
<th>{{$data->id_lokasi}}</th>
</tr>
#endforeach
You can get data from database in view also
#php( $contacts = \App\Contact::all() )
#php( $owners = \App\User::all())
<select class="form-control" name="owner_name" id="owner_name">
#foreach($contacts as $contact)
<option value="{{ $contact->contact_owner_id }}">{{ $contact->contact_owner }}</option>
#endforeach
</select>
It would be better if you pass data from controller to view.
return view('greetings', ['name' => 'Victoria']);
Checkout the docs:
https://laravel.com/docs/8.x/views#passing-data-to-views
Alternatively you can use #forelse loop inside laravel blade
#forelse($name as $data)
<tr>
<th>{{ $data->id}}</th>
<th>{{ $data->name}}</th>
<th>{{ $data->age}}</th>
<th>{{ $data->address}}</th>
</tr>
#empty
<tr><td colspan="4">No record found</td></tr>
#endforelse
In your controller:
$select = DB::select('select * from student');
return view ('index')->with('name',$select);
In Your view:
#foreach($name as $data)
<tr>
<th>{{ $data->id}}</th> <br>
<th>{{ $data->name}}</th> <br>
<th>{{ $data->age}}</th> <br>
<th>{{ $data->address}}</th>
</tr>
#endforeach
I hope this can help you.
#foreach($petani as $p)
<tr>
<td>{{ $p['id_user'] }}</td>
<td>{{ $p['username'] }}</td>
<td>{{ $p['alamat'] }}</td>
<td>{{ $p['no_telp'] }}</td>
<td>{{ $p['id_lokasi'] }}</td>
</tr>
#endforeach
**In side controller you pass this **:
$petanidetail = DB::table('tb1_user')->get()->toArray();
return view('listpetani', compact('petanidetail'));
and Inside view you use petanidetail variable as follow:
foreach($petanidetail as $data)
{
echo $data;
}
I hope you already know this, but try use Model and Controller as well,
Let the route take the request, and pass it to controller
and use Eloquent so your code can be this short:
$petani = PetaniModel::all();
return view('listpetani', compact('petani));
and instead using foreach, use forelse with #empty statement incase your 'listpetani' is empty and give some information to the view like 'The List is Empty'.
Try this:
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
on your view iterate $petani using foreach() like:
foreach($petani as $data)
{
echo $data->id_user; // $petani is a Std Class Object here
echo $data->username;
}
Other answers are correct, I just want to add one more thing, if your database field has html tags then system will print html tags like < p > for paragraph tag. To remove this issue you can use below solution:
You may need to apply html_entity_decode to your data.
This works fine for Laravel 4
{{html_entity_decode($post->body)}}
For Laravel 5 you may need to use this instead
{!!html_entity_decode($post->body)!!}

How do I pass a variable from a link to a controller in laravel?

So I have a page setup with a table of items. In each row there is a link. When the link is clicked I want to pass the ID of them through to the controller so I can pass it on to another view. Although I can't figure out how to do this.
This is the code in my item view
#foreach($items as $item)
<tr>
<td>{{$item->title}}</td>
<td>{{$item->genre}}</td>
<td>{{$item->description}}</td>
<td>View</td>
</tr>
#endforeach
As you can see there is a link which leads me to the rent view. In the controller this is all I have.
public function rent()
{
return view('rent');
}
Any help would be appreciated thanks.
I'd probably do it something like this.
#foreach($items as $item)
<tr>
<td>{{$item->title}}</td>
<td>{{$item->genre}}</td>
<td>{{$item->description}}</td>
<td>View</td>
</tr>
#endforeach
And then inside your controller you can accept the the value you are passing.
public function rent($value)
{
return View::make('new-view')->with('value', $value);
}
and then inside your new-view.blade.php
<p> The value I passed is: {{ $value }} </p>
Read more about Laravel url helpers here https://laravel.com/docs/5.1/helpers#urls
You can use route() helper:
#foreach($items as $item)
<tr>
<td>{{ $item->title }}</td>
<td>{{ $item->genre }</td>
<td>{{ $item->description }}</td>
<td>View</td>
</tr>
#endforeach
As alternative you can use link to action:
{{ action('RentController#profile', ['id' => $item->someId]); }}
And sometimes it's useful to use url() helper:
{{ echo url('rent', [$item->someId]) }}
More on these helpes here.
If you're using Laravel 5 with Laravel Collective installed or you're on Laravel 4, you can use these constructions to generate URLs with parameters.
PS: If you're using tables for layout, don't do it. You should learn DIVs, because it's really easy now to build cool layout with DIVs using Bootstrap framework which is built-in Laravel.

Categories