I have my User Table, which has a ID and a comes_from (id), which points to the id column of the same user table.
for example:
User
id, comes_from
1, null
2, 1
So user with ID 2 comes from user with ID 1
Now I'm trying to build this.
I want to print out a table with all users I have and another table column which gives me the email of the user, that the current row user comes from.
My User model:
public function recruited()
{
return self::findOrFail($this->comes_from)->pluck('email', 'id');
}
Now in my Controller:
public function recruitedUsersList()
{
return view('admin.recruitedUsers')->with('users', User::all()->with('recruited'));
}
This tells me "Call to undefined method with"
In my blade:
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->recruited()->email }}</td>
</tr>
#endforeach
Well.. this doesn't work for me. Does someone has a idea?
The Idea is:
A user can come from only one person. He get the "comes_from (id) as he register himself"
A User can recruite as many people as he wants.
Note: I assume a person can recruit more than one other person in your application. So I think hasMany relationship best describes this.
Update the function in your User Model
public function recruited()
{
return $this->hasMany('App\User', 'comes_from');
}
Update your controller to this
public function recruitedUsersList()
{
return view('admin.recruitedUsers')->with('users', User::with('recruited')->get());
}
Update your view logic to this
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->name }}</td>
<td>
#foreach($user->recruited as $recruit)
{{ $recruit->email }},
#endforeach
</td>
</tr>
#endforeach
Related
I am trying to read data from a local collections from my mongodb database. Name of database Intuit name of collection post.
Code inside PostController:
<?php
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller{
public function home() {
$posts = Post::with(['user'])->get();
return view('home', ['posts' => $posts->toArray()]);
}}
Code inside Post:
class Post extends Model{
use HasFactory;
protected $fillable = [
'STATUS',
'DDC_CODE',
'TRADE_NAME',
'SCIENTIFIC_CODE',
'SCIENTIFIC_NAME',
'INGREDIENT_STRENGTH',
'DOSAGE_FORM_PACKAGE',
'ROUTE_OF_ADMIN',
'PACKAGE_PRICE',
'GRANULAR_UNIT',
'MANUFACTURER',
'REGISTERED_OWNER',
'UPDATED_DATE',
'SOURCE'
];}
All of the above are the titles of the columns in the table.
Next home.blade.php:
<!DOCTPE html>
<html>
<head>
<title>View Records</title>
</head>
<body>
<table border = "1">
<tr>
<td>Id</td>
<td>STATUS</td>
<td>DDC_CODE</td>
<td>TRADE_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>SCIENTIFIC_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>INGREDIENT_STRENGTH</td>
<td>DOSAGE_FORM_PACKAGE</td>
<td>ROUTE_OF_ADMIN</td>
<td>PACKAGE_PRICE</td>
<td>GRANULAR_UNIT</td>
<td>MANUFACTURER</td>
<td>UPDATED_DATE</td>
<td>SOURCE</td>
</tr>
#foreach ($posts as $post)
<tr>
<td>{{ $post->_id }}</td>
<td>{{ $post->STATUS }}</td>
<td>{{ $post->DDC_CODE }}</td>
<td>{{ $post->TRADE_NAME }}</td>
<td>{{ $post->SCIENTIFIC_CODE }}</td>
<td>{{ $post->SCIENTIFIC_NAME }}</td>
<td>{{ $post->INGREDIENT_STRENGTH }}</td>
<td>{{ $post->DOSAGE_FORM_PACKAGE }}</td>
<td>{{ $post->ROUTE_OF_ADMIN }}</td>
<td>{{ $post->PACKAGE_PRICE }}</td>
<td>{{ $post->GRANULAR_UNIT }}</td>
<td>{{ $post->MANUFACTURER }}</td>
<td>{{ $post->REGISTERED_OWNER }}</td>
<td>{{ $post->UPDATED_DATE }}</td>
<td>{{ $post->SOURCE }}</td>
</tr>
#endforeach
</table>
</body>
</html>
The route handler is as follows
Route::get('view','App\Http\Controllers\PostController#home');
all of this seems to be fine but when I load up the application I see nothing but the column titles not sure what I am missing, I believe it is not working because the query in PostController is incorrect and need to change it as it is taken from another projec. Any and all help and suggestions are welcome.
Instead of
Post::with(['user'])->get();
use
Post::with(['user'])->all();
subject to user relationship is properly defined in your Post model
You are sending data as array, But you are trying to get them as object in your blade view file.
Instead of getting data like this:
$post->STATUS
Try using like this:
$post['STATUS']
Don't use ->toArray()
Pass data as below & variables will get displayed
return view('home', ['posts' => $posts)]);
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
I'm currently working on Laravel for the first time and i'm stuck with an issue. I have an export excel button on my tickets list page but It exports all the tickets ever created. Is there a way to only export weekly tickets in excel format based on the current date?
excel form :
#foreach (\App\Tickets::All() as $ticket)
<tr>
<td>{{ $ticket->societe}}</td>
<td>{{ $ticket->intervenant}}</td>
<td>{{ $ticket->assistance->level}}</td>
<td>{{ $ticket->message}}</td>
<td>{{ $ticket->urgence->niveau}}</td>
<td>{{ $ticket->statut}}</td>
<td>{{ $ticket->utilisateur->name}}</td>
</tr>
#endforeach
Excel function in ticketscontroller :
public function exportxls(){
Excel::create('tickets', function($excel){
$excel->sheet('tickets', function($sheet){
$sheet->loadView('export.ticketsexcel');
})->export('xls');
});
return redirect('/');
}
Thanks in advance.
You should have sent data from controller to the view and export only weekly tickets you can modify your query.
public function exportxls(){
$weeklyTickets = \App\Tickets::whereBetween('created_at', array(date("Y-m-d", strtotime("-7 days")), date('Y-m-d'))->get();
Excel::create('tickets', function($excel) use ($weeklyTickets){
$excel->sheet('tickets', function($sheet) use ($weeklyTickets){
$sheet->loadView('export.ticketsexcel', array('weeklyTickets' => $weeklyTickets));
})->export('xls');
});
return redirect('/');
}
In this function change "created_at" field according to your database field.
And in view you can modify it like this:
#foreach ($weeklyTickets as $ticket)
<tr>
<td>{{ $ticket->societe}}</td>
<td>{{ $ticket->intervenant}}</td>
<td>{{ $ticket->assistance->level}}</td>
<td>{{ $ticket->message}}</td>
<td>{{ $ticket->urgence->niveau}}</td>
<td>{{ $ticket->statut}}</td>
<td>{{ $ticket->utilisateur->name}}</td>
</tr>
#endforeach
Hope this helps you.
Compare two different columns from two different tables with if statement and show other column's data from either table in laravel 5.2
controller like:
public function labDetails(){
$users = DB::table('users')->lists('lab_name');
$labdetails = Labdetails2::paginate(20);
return View('labdetails')
->with('users', $users)
->with('labdetails', $labdetails);
}
here i want to match users table's 'lab_name' column with labdetails2 table's 'lab_name' column and if they matched show only the related data from labdetails table.
N.B. I am working with an old database where there's no relationship among tables.
views like:
<form action="" method="">
<select name="state" onchange="getValue(this)">
<option value="">Select Lab</option>
#foreach ($users as $key => $user)
<option value="{{ $user }}">{{ $user }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
#foreach ($labdetails as $labdetail)
<tbody>
<tr>
<td>{{ $labdetail->sl }}</td>
<td>{{ $labdetail->lab_name }}</td>
<td>{{ $labdetail->pc_name }}</td>
<td>{{ $labdetail->pc_ip }}</td>
<td>{{ $labdetail->mac1 }}</td>
<td>{{ $labdetail->assetno }}</td>
<td>{{ $labdetail->pc_type }}</td>
<td>{{ $labdetail->processor }}</td>
<td>{{ $labdetail->motherboard }}</td>
<td>{{ $labdetail->ram }}</td>
<td>{{ $labdetail->hdd }}</td>
<td>{{ $labdetail->location }}</td>
<td>{{ $labdetail->department }}</td>
<td>{{ $labdetail->entrydate }}</td>
<td>{{ $labdetail->comment }}</td>
</tr>
</tbody>
#endforeach
Please help me what should I do with the controller & views...
You can retrieve only the matches record this way
$users = DB::table('users')->lists('lab_name'); // this retrieves all lab_name as array.
Then, to retrieve only the matches labdetails
$labdetails = Labdetails2::whereIn('lab_name',$users)->paginate(20);
DB::table('users')
->join('labdetails2', 'users.lab_name', '=', 'labdetails2.lab_name')
->select('labdetails2.lab_name', 'labdetails2.pc_name')
->get()
in select(), white down the fields you required.
Here, labdetails2 and users are the table name.
this code finally worked for me:
public function showLabDetails(Request $request){
$q = $request -> request -> all();
$labdetails = Labdetails2::whereIn('lab_name', $q)->get();
return View('showlabdetails')
->with('labdetails', $labdetails);
}
thanks to everyone for helping me.
I'm new in Laravel. I'm storing 3 types of user in my users table:
admin : user_type_id = 1
agent : user_type_id = 2
farmer : user_type_id = 3
user_type_id column is in the users table.
In one of my Blade files, I want to only show the names of the agents. Here is my foreach loop, which is importing all 3 types of the user (it's showing the names of admins, agents and farmers).
#foreach($farmerPoint->user as $agent)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endforeach
This is probably more of a logic issue than a Laravel issue. NOTE that I would suggest you limit the $agents instead using your query (where) rather than this way, BUT:
#foreach($farmerPoint->user as $agent)
#if ($agent->user_type_id === 2)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endif
#endforeach
You can use a simple blade #if() statement like so:
#foreach($farmerPoint->user as $agent)
#if ($agent->user_type_id === 2)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endif
#endforeach
Or you can use a collection where() since all eager / lazy loaded relations are returned in collections:
http://laravel.com/docs/5.1/collections#method-where
#foreach($farmerPoint->user->where('user_type_id', 2) as $agent)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endforeach