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);
Related
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)
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>
View Code:
<li class="active"><a href="#tab_1" data-toggle="tab" id="versiontab"
onclick="window.location='{{ url("versiondb") }}'" >Version
Database</a></li>
This is my view code.This is also a tab .Onclicking the tab the url redirection works.
Route Code:
Route::get('versiondb','versiondbController#select');
Controllercode:
public function select()
{
$users=debModel::all();
return redirect()->back()->with($users);
}
Here it should fetches the values from db and redirecting back to the same page and display the retreived values in table .But i am getting an error here as $users undefined
Inside the Same view (specified above):
<table class="table" id="table">
#foreach($users as $users)
<thead>
<tr class="header">
<th valign="middle" width="3%">Versions</th>
<th valign="middle" width="2%">Supported versions</th>
<th valign="middle" width="10%">Release Date</th>
<th valign="middle" width="3%">Type</th>
<th valign="middle" width="10%">Description</th>
<th valign="middle" width="3%">Actions</th>
<th valign="middle" width="3%">Downloader</th>
<!--<th valign="middle" width="3%">Beta code</th>-->
</tr>
</thead>
<tbody>
<tr>
<td>{{$users->versions}}</td>
<td></td>
</tr>
#endforeach
</tbody>
</table>
I think this is the place where error occurs.
When you use "->with" in Laravel, Laravel go back to that page with a session message named with.
Usiing with in your scenario would be
public function select()
{
$users=debModel::all();
return redirect()->back()->with("message","I generated all users");
}
which would be accessed in your blade as
#if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
The solution to your question is write it this way
public function select()
{
$data['users']=debModel::all();
return view("viewdeb",$data);
}
then your blade you will need a foreach loop
#foreach ($users as $user)
{{$user->firstname}}
#endforeach
mind you, more data can be sent back to the view by just including them in the data array
public function select()
{
$data['users']=debModel::all();
$data['business']=businessModel::all();
return view("viewdeb",$data);
}
The issue is here:
#foreach($users as $users)
here your array variable name and the variable in which you are holding the data on each iteration is the same, so change it to:
#foreach($users as $user)
<td>{{$user->versions}}</td>
Use ($users as $user) instead ($users as $users) here
#foreach($users as $user)
And in your template
<td>{{$user->versions}}</td>
Also change in your controller
return view('haghwayUpdate')->with('users',$users);
Hi Guy's Im always getting an error from laravel 5.4.
Im creating a list of members... but when i use #foreach it says
ErrorException in 6163a8b030c7474bc8eaad359ab99eb61ebdb127.php line 38:
Trying to get property of non-object (View: E:\wamp64\www\gplspring2017\resources\views\admin\memberlist.blade.php)
Here's my controller
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->first();
$count = count($teams);
if (!$count) {
return redirect('404');
} else {
return view('/admin/memberlist', ['team' => $teams]);
}
}
and here's my view code:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($qmember as $get_member)
<tr>
<td><img src="{{ $get_member->member_pic }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>
When i remove the foreach the code is working.. but i try to add the #foreach($qmember as $get_member) it not working anymore...
Check this line:
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
when you are using first() then it does not return an Std Class object. If you want it so then use get()
Using first() method you are fetching only one record from DB that match with the ID (First one matched). You need to use get().
Controller:
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->get(); //Use get() if you expect more than one result not first()
$count = count($teams);
if(! $count)
{
return redirect('404');
}
else
{
return view('/admin/memberlist', ['teams' => $teams]);// this team variable must be used in the blade
}
}
View:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($teams as $team)
<tr>
<td><img src="{{ $team['member_pic'] }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>
I using tablesorter in twitter-bootstrap. This works fine when using like this:
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped tablesorter">
<thead>
<tr>
<th>Brugernavn <i class="fa fa-sort"></i></th>
<th>Rolle <i class="fa fa-sort"></i></th>
<th>Oprettet <i class="fa fa-sort"></i></th>
</tr>
</thead>
<tbody>
<tr>
<td>3326</td>
<td>10/21/2013</td>
<td>3:29 PM</td>
</tr>
<tr>
<td>3325</td>
<td>10/21/2013</td>
<td>3:20 PM</td>
</tr>
<tr>
<td>3324</td>
<td>10/21/2013</td>
<td>3:03 PM</td>
</tr>
<tr>
<td>3323</td>
<td>10/21/2013</td>
<td>3:00 PM</td>
</tr>
<tr>
<td>3322</td>
<td>10/21/2013</td>
<td>2:49 PM</td>
</tr>
</tbody>
</table>
This works and my Console shows:
event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery-1.10.2.js:5
Evaling expression:var sortWrapper = function(a,b) {var e0 = (a[0] === null && b[0] === null) ? 0 :(a[0] === null ? Number.POSITIVE_INFINITY : (b[0] === null ? Number.NEGATIVE_INFINITY : a[0] - b[0]));if(e0) { return e0; } else { return a[4]-b[4];}; return 0; }; ,0ms jquery.tablesorter.js:147
Sorting on 0,0 and dir 0 time:,1ms jquery.tablesorter.js:147
Rebuilt table:,0ms
But when I use data from my Mysql database and loop through, it does not work.
I do it like this:
<table class="table table-bordered table-hover table-striped tablesorter">
<thead>
<tr>
<th>Brugernavn <i class="fa fa-sort"></i></th>
<th>Rolle <i class="fa fa-sort"></i></th>
<th>Oprettet <i class="fa fa-sort"></i></th>
</tr>
</thead>
<?php
foreach ($users as $key => $value) {
?>
<tbody>
<tr data-id="<?php echo $value['user'];?>" data-toggle="modal" data-target="#edit" class="open-edit">
<td data-id="username" class="username-edit"><?php echo $value['user'];?></td>
<td class="username-edit" data-id="role"><?php echo $value['role'];?></td>
<td><?php echo $value['lastlogin'];?></td>
</tbody>
<?php
}
?>
</table>
The data gets show correct but when I click the sort button nothing happends. The Console shows:
event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery-1.10.2.js:5
Evaling expression:var sortWrapper = function(a,b) {var e0 = (a[0] == b[0] ? 0 : (a[0] === null ? Number.POSITIVE_INFINITY : (b[0] === null ? Number.NEGATIVE_INFINITY : (a[0] < b[0]) ? -1 : 1 )));if(e0) { return e0; } else { return a[3]-b[3];}; return 0; }; ,0ms jquery.tablesorter.js:147
Sorting on 0,0 and dir 0 time:,1ms jquery.tablesorter.js:147
Rebuilt table:,0ms
Can anyone help me understand why the sort function does not work when using my php loop?
I solved it. I needed to keep <tbody></tbody> outside the loop like this:
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped tablesorter">
<thead>
<tr>
<th>Brugernavn <i class="fa fa-sort"></i></th>
<th>Rolle <i class="fa fa-sort"></i></th>
<th>Oprettet <i class="fa fa-sort"></i></th>
</tr>
</thead>
<tbody>
<?php
foreach ($users as $key => $value) {
?>
<tr data-id="<?php echo $value['user'];?>" data-toggle="modal" data-target="#edit" class="open-edit">
<td data-id="username" class="username-edit"><?php echo $value['user'];?></td>
<td class="username-edit" data-id="role"><?php echo $value['role'];?></td>
<td><?php echo $value['lastlogin'];?></td>
<?php
}
?>
</tbody>
</table>