PHP append a variable to the end of a string - php

So here I want to append the id of the currently logged in user to the name of a table, like for example Rating_User_1, but the 1 here comes from the id of the logged in user. I assigned the id of the logged in user to $id, as shown below in the code, and append it to the table column name, but this doesn't work. I tried echoing the $id by itself and it works (i.e. it spits out the id of the logged in user but that's not what I want), but when I try to append it to the string in order to "emulate" the table name it doesn't work. Any tips on workarounds?
#foreach($data as $data)
<tr>
<td> {{$loop->iteration}} </td> <!-- add number to the row -->
<td>
<a href="{{ Url('reply_thread') }}/{{ $data->id }}" class="text" >{{ $data->Judul }}</a>
</td>
<td><?php $id = Auth::user()->id;
echo $data->Rating_user_[$id]; ?> </td>
</tr>
#endforeach
My controller:
function threads() {
$data=Thread::all();
return view('thread.thread', ['data'=>$data]);
}
and some of the tables in the table Thread has columns named Rating_user_1, Rating_user_2, etc.

Variable expansion sometimes needs curly braces to function properly. The following should work:
echo $data->{'Rating_user_' . $id}
See Variable Parsing - Complex Syntax for info on "curly brace" variable expansion.
While it doesn't look like you need it here, Variable Variables may also be useful to read up on. (aka, $a = 'b'; $b = 'hello'; echo $$a; // Same as "echo $b", outputs 'hello')

You could try:
#foreach($data as $data)
<tr>
<td> {{$loop->iteration}} </td> <!-- add number to the row -->
<td>
<a href="{{ Url('reply_thread') }}/{{ $data->id }}" class="text" >
{{ $data->Judul }}
</a>
</td>
<td>{{ $data->{'Rating_user_' . $id} }}</td>
</tr>
#endforeach

You have multiple options to achieve this, the first would be to call the property/method with the name stored in a variable:
$tableName = 'Rating_user_' . $id;
$tableName = "Rating_user_$id";
$data->$tableName;
You could also use curly braces to make a hot concat on the property/method call itself as #romellem suggested:
$data->{'Rating_user_' . $id}
$data->{"Rating_user_$id"}
If you are going to access the $data->Rating_user more than once I would recommend you the first approach, if not the second one is okey aswell.

I think that your code is almost correct, and I think it's better if you use double curly than use echo like:
#foreach($data as $data)
<tr>
<td> {{$loop->iteration}} </td> <!-- add number to the row -->
<td>
<a href="{{ Url('reply_thread') }}/{{ $data->id }}" class="text" >{{ $data->Judul }}</a>
</td>
<td>{{ $data->{'Rating_user_' . Auth::id()} }}</td>
</tr>
#endforeach

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

How I build if statement if the column is exit in database or not (Laravel)

How I can write if the statement if the find the attachment in the column or not?
#if($request->pdfuplode)
<td>
<a href="{{ url('uploads/reports/'.$request->pdfuplode) }}" target="_blank">
report
</a>
</td>
#else
<td> - </td>
I mean if the pdfuplode is not null will show the link pf report .. if it null show (-)
You just do an empty() check in your blade like so:
#if(empty($request->pdfuplode))
<td> - </td>
#else
<td>
<a href="{{ url('uploads/reports/'.$request->pdfuplode) }}" target="_blank">
report
</a>
</td>
#endif
i think you are trying to check whether the request contains your file or not
#if(!$result->isEmpty())
// $array is not empty
#else
// $array is empty
#endif
isEmpty() method will check if the array is null or not . if its not null then the if condition will run otherwise the else condition. You can make changing in the if else for your desired work.

Laravel 5 getting ID from URL

I have a table view in which I can click an button icon and redirect to another page carrying the id of the row that has been clicked.
#foreach ($patients as $patient)
<tr>
<td>{{ $patient->pID }}</td>
<td>{{ $patient->pName }}</td>
<td>{{ $patient->pAddress }}</td>
<td>{{ $patient->pBday }}</td>
<td>{{ $patient->pPhone}}</td>
<td>{{ $patient->pEcon }}</td>
<td>{{ $patient->pDreg }}</td>
<td></td>
<td>
<a href="{{ URL::to('visit/'.$patient->pID) }}">
<img src="../images/viewvisit.png" style="width:30px; height:30px;">
</a>
</td>
<td>
<a href="{{ URL::to('addeditvisit/'.$patient->pID) }}">
<img src="../images/visit.png" style="width:30px; height:30px;">
</a>
</td>
<td>
<a href="{{ URL::to('editpatient/'.$patient->pID) }}">
<img src="../images/update.png" style="width:30px; height:30px;">
</a>
</td>
<td>
<a href="{{ URL::to('deletepatient/'.$patient->pID) }}">
<img src="../images/delete.png" style="width:30px; height:30px;">
</a>
</td>
</tr>
#endforeach
what I want is to get the id from the URL and put it in a variable so that I can utilize it my other page.
I'm currently stuck with this controller function.
public function index(Request $request) {
$doctors = Doctor::where('dStatus', 0)
->lists('dName', 'dID');
$beds = Bed::where('bStatus', 0)
->lists('bName', 'bID');
$patient = Patient::patient();
// $uri = $request->path('patient');
return View::make('pages.addeditvisit', [
'doctors'=>$doctors,
'beds'=>$beds,
'patient'=>$patient->pID
]);
}
This is late. But for the benefit of others like me;
If you do not have to do it in a method like the answers above have shown, As of Laravel 5.0 (Not sure about previous versions), you can do
$request->route('id');
That returns the value of the id parameter on the route.
Or just use it within Blade: {{ request()->route('id') }}
Basically when you are defining the routes, you use something called route parameters, something like this
Route::get('/visit/{id}', 'Controller#someMethod');
This id will be available as a parameter in your handler funtion,
public function someMethod($id) {
// you have the id here
}
Simple example:
as link=> example.com/user/1
as rout=> Route::get('user/{id}', 'UserController#user');
as UserController function
public function user($id){
echo $id;
}
output => 1
The trick is to declare the url's structure at routes including the id, for example:
// {{ URL::to('editpatient/'.$patient->pID) }}
Route::get('editpatient/{patientId}', 'MyController#index');
Then, just inject the id in the controller's function:
public function index($patientId){
// $patientId is the variable from url
}
Please refer to the answered question for how to get a parameter from a route. But if you stumbled on this old Laravel thread looking for how to retrieve a model by its ID, the simplest way is to instantiate the model in the controller's request parameter:
Route::get('/retrieve_product/{product}', 'SearchController#getProduct');
Then over in your controller, you simply need:
use App\Product;
class SearchController extends Controller
{
public function getProduct( Product $product ) {
return $product;
}
}
That's it. No find, no where, no get, no first etc.
So, in this example, if you visit /retrieve_product/1 the first product is returned to you.
Route::get('post/user/{id}','ProductController#allpost')->where('id', '[0-9]+');
Controller
public function allpost($id)
{
$products = Product::where('uploadby', $id)->orderBy('created_at','desc')->paginate(5); <br>
return view('product.user_product')->with('products', $products);
}

Displaying multiple columns of data [from a PHP list] in a table in Laravel View

I currently am using a Laravel View with the following HTML that displays data onto a table using the <td> tag.
#if($unrostered)
<table class="table table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
#endif
I am currently just using one associative list called $unrostered which contains the key and value called name and profile. This shows two columns of data, like this:
As you can see, the name variable is the name of the person, and the profile is a URL that links to their profile.
My HTML skills aren't good. I want to pass in multiple lists [of the same length] into my HTML table, so each list occupies their own column.
I tried juggling the row and data tags, however, all I was able to achieve were that all the data points occupied only one row instead of columns.
Assume the name of your other lists are $list1, $list2, ... and they have the same format as your $unrostered. Then your foreach code should be changed to:
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list1[$name] }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list2[$name] }}">{{ $name }}
</a>
</td>
</tr>
#endforeach

2 Foreach Loop Inside A Table

I had Users table and group table, when i load a group table, it loads the user which have same group_id as the group table id. it works, but my problem is the foreach was quite a mess. the output looks like this..
however, i want to make the output look like this.
here is my code..
<tbody>
<?php $i = 0; ?>
#foreach ($auser as $res)
<?php $i += 1; ?>
<tr #if($i%2==0) class="bgcolor" #endif>
<td>{{ $res->id }}.</td>
<td id="showdet">{{ $res->nama }}</td>
<td>{{ $res->description }}</td>
<?php $user = DB::table('users')->where('devgroup',$res->id)->get();?>
#foreach($user as $mem)
<td>{{ $mem->name }}</td>
#endforeach
<td>
<a class="default-btn" href="/pms/admin/group/edit/{{ $res->id }}">Edit</a>
<a type="submit" class="default-btn del" data-id="devgroup" href="#"{{ $res->id }}">Delete</a>
</td>
</tr>
#endforeach
</tbody>
what did i do wrong? thank you for your help.
You create new TD for each member. The nested foreach has to be:
<td>
#foreach($user as $mem)
{{ $mem->name }}<br>
#endforeach
</td>
The result will be:
<td>
Name 1<br>
Name 2<br>
Name 3<br>
</td>
I don't know the template engine you used, add inside a loop condition and don't put <br> after the last one member.

Categories