How to get id from the task in which I click - php

So I have "tasks", and each task can have multiple notes.
I display tasks like this:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Task Id</th>
<th>Project</th>
<th>Task title</th>
<th>Description</th>
<th>Status</th>
<th>Priority</th>
<th>Created by</th>
<th>Created on</th>
#if (Auth::user()->role=='admin')
<th>Admin</th>
#endif
</tr>
</thead>
<tbody class="">
#foreach ($task as $task)
<tr>
<td>{{$task->task_id}}</td>
<td>{{$task->project->proj_title}}</td>
<td>{{$task->task_title}}</td>
<td>{{$task->task_desc}}</td>
<td>{{$task->status}}</td>
<td>{{$task->priority}}</td>
<td>{{$task->user->name}}</td>
<td>{{$task->created_at}}</td>
<td>
<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="{{route('tasks.notes',$task)}}">Notes</a>
Each task is a row and as you can see at the end there is a button which gets the user to the notes view.
I need to get the task_id from the task you click on, so I can assign that task_id to the note, this way every task has its own notes.
This are my relations between "Task" and "Note";
Task model:
public function notes(){
return $this->hasMany('App\Note','task_id');
}
Note model:
public function task(){
return $this->belongsTo('App\Task','task_id');
}
And this is where I display the notes:
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Note</th>
</tr>
</thead>
<tbody>
#foreach($notes->where('task_id',$task->task_id) as $note)
<tr>
<td>Created by {{$note->user}}<td>
<td>{{$note->note}}</td>
</tr>
#endforeach
</tbody>
</table>
My NoteController index function:
public function index(Task $task)
{
$task_id = $task['task_id'];
return view('notes.index', [
'notes' => Note::all(),
'user' => User::all(),
'task' => $task_id,
]);
}
Thanks in advance

Send the id instead of the whole object in the blade like :
<a class="dropdown-item" href="{{route('tasks.notes', $task->id)}}">Notes</a>
Then in the index action receive it and get the related task notes like :
public function index($task_id)
{
$task = Task::find($task_id);
return view('notes.index', [
'notes' => Note::all(),
'user' => $task->notes,
'task' => $task,
]);
}
In the notes blade, you've just to loop through them :
#foreach($notes as $note)

Related

Can't display the fetch data

I am new in ReactJS so fetching data isn't my expertise, so I'm trying to learn ReactJS with my PHP Native. The problem is fetching data is difficult for me. This code below there's no error but it won't display my data.
It can't display in my UI, the data is stayed in console(Network -> Response), i tried everything still can't output it. Can someone help?
import './Timesheet.css';
import React, { useState, useEffect } from 'react';
import { Table, Container, Button, } from 'react-bootstrap';
const Timesheet = () => {
const [item, setItem] = useState([]);
useEffect(() => {
fetch("http://localhost/WaterRefillingStation/app/controllers/timesheet_controller.php")
.then(response => response.json())
.then(
(result) => {
setItem(result);
}
)
}, [])
return (
<Container fluid>
<div className="d_flex my-4, text-uppercase">
<h1>
Timesheet Screen
</h1>
</div>
<div className="float-end, tryy" >
<Button variant="success" size="md">Time IN</Button>{' '}
<Button variant="secondary" size="md">Time OUT</Button>{' '}
<Button variant="danger" size="md">Logout</Button>
</div>
<Table striped bordered hover size="md" variant="dark" responsive="xl" >
<thead className="thead-dark">
<tr style={{textAlign: "center"}}>
<th>ID</th>
<th>Date</th>
<th>Profile Picture</th>
<th>Last Name</th>
<th>First Name</th>
<th>Designation</th>
<th>Time IN</th>
<th>Time OUT</th>
</tr>
</thead>
<tbody>
{item.map(item => (
<tr key={item}>
<td>{item.userID}</td>
<td>{item.birthday}</td>
<td>{item.profpic}</td>
<td>{item.lastname}</td>
<td>{item.firstname}</td>
<td>{item.designation}</td>
<td>{item.created_at}</td>
<td>{item.updated_at}</td>
</tr>
))}
</tbody>
</Table>
</Container>
);
}
export default Timesheet;
Ok, it seems that you have used .map in the wrong way. You can use it for an array, not an object. use this instead:
return (
<Container fluid>
<div className="d_flex my-4, text-uppercase">
<h1>
Timesheet Screen
</h1>
</div>
<div className="float-end, tryy" >
<Button variant="success" size="md">Time IN</Button>{' '}
<Button variant="secondary" size="md">Time OUT</Button>{' '}
<Button variant="danger" size="md">Logout</Button>
</div>
<Table striped bordered hover size="md" variant="dark" responsive="xl" >
<thead className="thead-dark">
<tr style={{textAlign: "center"}}>
<th>ID</th>
<th>Date</th>
<th>Profile Picture</th>
<th>Last Name</th>
<th>First Name</th>
<th>Designation</th>
<th>Time IN</th>
<th>Time OUT</th>
</tr>
</thead>
<tbody>
<tr key={item}>
<td>{item.userID}</td>
<td>{item.birthday}</td>
<td>{item.profpic}</td>
<td>{item.lastname}</td>
<td>{item.firstname}</td>
<td>{item.designation}</td>
<td>{item.created_at}</td>
<td>{item.updated_at}</td>
</tr>
</tbody>
</Table>
</Container>
if you want to have a table for each user (for example), you should to provide data like below in your backend:
{
data:[
{ USER1 Info},
{ USER2 Info},
// so on
]
}

Delete the row after removing file from storage

everything works perfectly but it throw an error “Trying to get
property ‘path’ of non-object” on this line
“unlink(“uploads/”.$img->path);” and when I refresh the row and file
are deleted .if there is another way please tell me
blade :
<table class="table table-hover">
<thead>
<tr>
<th scope="col">image</th>
<th scope="col">title</th>
<th scope="col">Actions </th>
</tr>
</thead>
<tbody>
#foreach($data as $img)
<tr>
<th scope="row"> <img src="uploads/{{ $img->path }}" width="50%" /></th>
<td>{{$img->title}}</td>
<td>
<a href='#'><i class="fa fa-edit" id="updateIcon" wire:click="selectItem({{ $img->id }}, 'update')" ></i></a>
<i class="fa fa-trash" style='color:red;' wire:click="selectItem({{ $img->id }}, 'delete')" data-target="#modalFormDelete"></i>
</td>
#endforeach
</tr>
class:
public function selectItem($itemId, $action)
{
$this->selectedItem = $itemId;
if ($action == 'delete') {
$this->dispatchBrowserEvent('openDeleteModal');
}
}
public function delete()
{
$img=Caroussel_Img::find($this->selectedItem);
unlink("uploads/".$img->path);//bug here
Caroussel_Img::where("id",$img->id)->delete();
$this->dispatchBrowserEvent('closeDeleteModal');
}
It is not a good idea,
and I don't know what is inside "Caroussel_Img"
But if it is removing the file add "#" at beginning of the line and it won't bother you anymore
#unlink("uploads/".$img->path);

Property [id] [details] [details] [amount] does not exist on this collection instance

On my controller take data via foreach where the $upcoming->order_id is same I am checked by dd($notes) data is show but i can't get on my view php. before I am tried $note->get('amount') method it is also not working
public function index()
{ $upcomings = Booking_informations::Upcoming();
$notes = [];
foreach ($upcomings as $upcoming) {
$notes[] = Notes :: where('order_id',$upcoming->order_id)-> orderBy('id', 'ASC')->get();
};
return View('upcoming',compact('upcomings','notes',));
}
upcoming.blade.php
<table class="table table-bordered mb-0">
<tbody>
#foreach($notes as $note )
<tr>
<th scope="row">1</th>
<td>{{date('d-M-Y', strtotime($note->created_at))}}</td>
<td>{{$note->details}} </td>
<td>{{$note->amount}} </td>
</tr>
#endforeach
</tbody>
</table>
I have got answer while wrote below method have any more simple way for get results..
<table class="table table-bordered mb-0">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Content</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach($notes as $note )
#foreach($note as $id )
<tr>
<td scope="row">1</td>
<td>{{$id->details}} </td>
<td>{{$id->amount}} </td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
Try referencing $note instead of $notes in your foreach loop and see if that works.
I have also noticed that you have named your view file View.blade.php. Should this be upcoming.blade.php to match the name you have provided in the returned view? Just a thought.

Laravel Blade not showing data from controller

I want to show data in tabular form in Laravel Blade View. Now, data is not showing up in blade, but when I check in controller using print_r it shows up Following is the code for Controller:-
public function index(){
$pickup = PickupLocation::select('*')
->whereNull('deleted_at')
->get()
->toArray();
$page = 'pickup_locations';
return view('backEnd.pickupManagement.index',compact('page'));
}
Following is the code in web.php:-
Route::match(['get','post'],'pickup-locations','backEnd\pickupManagement\PickupController#index');
And following is the code I am using View:-
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Address</th>
<th width="13%">Action</th>
</tr>
</thead>
<tbody>
#if(!empty($pickup))
#foreach($pickup as $key=>$value)
<tr>
<td>{{ ucfirst($value['name']) }}</td>
<td>{{ ucfirst($value['contact_no']) }}</td>
<td>{{ $value['location'] }}</td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
add $pickup variable as second argument in your compact method.
Try like this
return view('backEnd.pickupManagement.index',compact('page', 'pickup')); }
Happy codding

Passing data to controller on row bases

I have a table display and each row contains a button. With this button l have to pass the id to the controller where using this Admin can view user details one by one.
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<button type="submit" class="btn btn-
default" name="viewcv"> View</button> <br>
<br>
</td>
</tr>
#endforeach
</tbody>
</table>
Please check the code , hope it helps:
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>CV</td>
</tr>
#endforeach
</tbody>
</table>
Assuming you want to pass the field "id" or change it according to your requirement.
If you want to link to a show view, try something like this:
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<a href="{{action('JobController#show', $row->id)}}" class="btn btn-default">View</button>
</td>
</tr>
#endforeach
This will create a link to a view, where you can show the data.
In your controller JobController, you must create a function that receives the data:
public function show(Request $request, $id) {
...
}
Just pass the id of the record to controller function and get the details from database.
<td>
View <br>
</td>

Categories