Laravel - Cannot Un Delete and Display Data using Soft Delete - php

Hello I cannot un delete my data why is it? here is my code
on the Controller
public function displayArchive()
{
$clients = Client::withTrashed();
return view('admin.clients.homeArchive')->with('clients', $clients);
}
one the View
<table class="table table-bordered" id="dynamic_field">
<tr>
<th>Client Code</th>
<th>Client Name</th>
<th>Address</th>
<th>Tel No.</th>
<th>Contact Person</th>
<th>Mobile No.</th>
<th>Email Address</th>
<th>Website</th>
<th>Status</th>
<th>Update</th>
</tr>
#foreach ($clients as $client)
<tr>
<td>{{ $client->client_code }}</td>
<td>{{ $client->client_name }}</td>
<td>{{ $client->address }}</td>
<td>{{ $client->tel_no }}</td>
<td>{{ $client->contact_person }}</td>
<td>{{ $client->mobile_no }}</td>
<td>{{ $client->email_ad }}</td>
<td>{{ $client->website }}</td>
<td>Inactive</td>
<td></td>
</tr>
#endforeach
</table>
on the Web
Here you can see how I call my controller and view pages.
Route::get('/admin/clients/homeArchive', 'Admin\ClientsController#displayArchive');
EDITED
Here is the edited code please take a look
my Model
use SoftDeletes;
protected $dates = ['deleted_at'];
// Table Name
protected $table = 'clients';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;

Can you try with ->get() method.
public function displayArchive()
{
$clients = Client::withTrashed()->get();
return view('admin.clients.homeArchive')->with('clients', $clients);
}
NOTE: it don't undelete any data. it only get data with deleted rows.
If you want recover deleted data, you must use ->restore() method.
For all restored all data;
Link:
Inactive
Route:
Route::get('/admin/clients/restore-all', 'Admin\ClientsController#restoreAll')->name('admin.client.restore_all');
Controller action:
public function restoreAll(){
Client::withTrashed()->restore();
}
Restore row by row data;
Link:
Inactive
Route:
Route::get('/admin/clients/restore/{client}', 'Admin\ClientsController#restore')->name('admin.client.restore');
Controller action:
public function restore(Client $client){
$client->restore();
}
$client->id is the client collection, I think you want to inactive in the listed foreach row, is right?

Related

Undefined variable $consultation [duplicate]

This question already has answers here:
Variable undefined error in laravel blade view
(4 answers)
Closed last month.
I want display my data to my blade file but it show this error
Undefined variable $consultation
This is my controller
public function p_consultation()
{
$consultation = Consultation::all();
return view ('dashboard/admin_panel/p_consultation')->with( $consultation);
}
This is my Route
Route::get('dashboard/admin_panel/p_consultation', [PenconsultationController::class, 'Consultation'])
And this is my Blade File
<tr>
<th>#</th>
<th>Item Name</th>
<th>Dscriptiom</th>
<th>Material</th>
<th>Quantity</th>
<th>Start Date</th>
<th>End Date</th>
</tr>
</thead>
<tbody>
#foreach($consultation as $consultation)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $consultation->item_name }}</td>
<td>{{ $consultation->description }}</td>
<td>{{ $consultation->material }}</td>
<td>{{ $consultation->quantity }}</td>
<td>{{ $consultation->start_date }}</td>
<td>{{ $consultation->end_date }}</td>
</tr>
#endforeach
change from
return view ('dashboard/admin_panel/p_consultation')->with( $consultation);
to
return view ('dashboard/admin_panel/p_consultation', ['consultation' => $consultation]);
If you're passing data to the controller using with(), you need to bind it with an accessor.
->with('consultation', $consultation)
return view('dashboard/admin_panel/p_consultation')->with('consultation', $consultation);
You can use compact() as well
Example.
return view('greeting')
->with('name', 'Victoria')
->with('occupation', 'Astronaut');

Class "App\Http\Controllers\User" not found [duplicate]

This question already has answers here:
Class "App\Http\Controllers\Auth\User" not found
(2 answers)
Closed 1 year ago.
So I was fetching data from my database to print in a table however, it says that
Class "App\Http\Controllers\User" not found.
Here is the controller and here is how I will print the data
public function directory()
{
$dashboardTitle = "Directory";
$isCurrent = "Directory";
$users = User::all();
return view('dashboard.directory', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent,
'users' => $users
]);
}
table to print at
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th> Picture </th>
<th>Name</th>
<th>Email</th>
<th>User type</th>
<th>Birthdate</th>
<th>Contact number</th>
<th>Created time</th>
</tr>
#foreach($users as $user)
<tr>
<td>{{ $user->profile_image }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->user_type }}</td>
<td>{{ $user->bdate }}</td>
<td>{{ $user->contact_no}}</td>
<td>{{ $user->created_at }}</td>
</tr>
#endforeach
</thead>
<tfoot>
</tbody>
</table>
At the top off your controller add
Laravel 8+
use App\Models\User;
Laravel <=7
use App\User;

How to use pluck to get specific data from other table in Laravel?

From SalesController, I want to get specific name from table Item and passed to HTML.
My controller looks like
public function index()
{
$items=Item::orderBy('name','ASC')->get()->pluck('name','id');
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales,'items',$items);
}
My HTML is
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Quantity</th>
<th>Total Price</th>
<th>Detail</th>
</tr>
</thead>
<tbody>
#foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->items->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
</tr>
#endforeach
</tbody>
But I get the following error after trying to access the page:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\inventoryApp\resources\views\Sale\index.blade.php)
there are two way
1.Using relationship
class Sale extends Model
{
public function item()
{
return $this->belongsTo('App\Item','item_id','id');
}
}
so you can use like below
<td>{{ $sale->item->name }}</td>
2. Using array data
public function index()
{
$items=Item::orderBy('name','ASC')->pluck('name','id')->toArray();
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales,'items',$items);
}
<td>{{ $items[$sale->item_id] }}</td>
$items=Item::orderBy('name','ASC')->pluck('name','id');
and use $items directly without sale object
If you have relationship with table Sale.
Add this function to your Sale Class.
public function item()
{
return $this->belongsTo('App\Item');
}
In your controller you can use compact
public function index()
{
$items=Item::orderBy('name','ASC')->get();
$sales=Sale::all();
return view('Sale.index')->compact('sales','items');
}
And you can use Eager load to your HTML.
<tbody>
#foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->item->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
</tr>
#endforeach
</tbody>
You are getting this error because you are trying to get a property of item name that does not exist on the sale object. you can simply do it by eloquent relationship as follows:
On your sale model:
public function item()
{
return $this->belongsTo('App\Item','item_id');
}
On your controller:
public function index()
{
$sales=Sale::all();
return view('Sale.index',compact('sales'));
}
Then on your blade you can easily get related Item name:
#foreach($sales as $sale)
<tr>
<td>{{ $sale->item->name }}</td>
</tr>
#endforeach

'Undefined variable: clients (View: C:\wamp\www\myLSF\resources\views\back\clients\index.blade.php)'

I'm creating a index.blade.php in view/back/client to list the clients.but I'm having this error.
Undefined variable: clients (View:
C:\wamp\www\myLSF\resources\views\back\clients\index.blade.php)
here is my index.blade.php
<table id="example1" class="table table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>First Name </th>
<th>Last Name</th>
<th>Address</th>
<th>Postal Code</th>
<th>City</th>
<th>Province</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
#foreach($clients as $client)
<tr>
<td>{{ $client->id }} </td>
<td>{{ $client->firstname }}</td>
<td>{{ $client->lastname }}</td>
<td>{{ $client->address }}</td>
<td>{{ $client->postalcode }}</td>
<td>{{ $client->city }}</td>
<td>{{ $client->province }}</td>
<td>{{ $client->phoneno }}</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>First Name </th>
<th>Last Name</th>
<th>Address</th>
<th>Postal Code</th>
<th>City</th>
<th>Province</th>
<th>Phone</th>
</tr>
</tfoot>
</table>
here is my clientcontroller.php
class clientController extends Controller
{
public function list()
{
$clients = client::get();
return view('/back/clients/index', compact('clients'));
}
}
here is my client.php model
class client extends Model
{
use Notifiable;
#var
protected $table='clients';
protected $fillable=
['firstname','lastname','address','postalcode',
'city','province','phoneno'];
}
here i set the route for this in web.php
Route::name('clients')->get('clients', 'clientController#list');
You need to import your client model into the clientController.php file.
To do this, add use App\client at the top, like so:
use App\client;
class clientController extends Controller
{
public function list()
{
$clients = client::get();
return view('/back/clients/index', compact('clients'));
}
}

Select List From Database With Parameter

Hi there i have some problem getting list from database using laravel 4.1
I have session that stored some value about companyid. Then i want to print the list to table based on the companyid itself. Let say i can access the session using {{ Session::get('name')}}
This is my controller index()
public function index($cid) {
$pengguna = User::where('id_company', '=', $cid)->get();
$pengguna->toarray();
$data = array(
'pengguna' => $pengguna,
'page' => 'master',
'index' => 0
);
return View::make('console.pengguna')->with($data);
}
then i want to print the data to table in view
#if($pengguna->count())
<table class="table table-striped table-bordered" id="table_pengguna_list">
<thead>
<tr>
<th>No</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Username</th>
<th>Email</th>
<th>Status</th>
<th>Last Login</th>
<th class="td-actions">Action</th>
</tr>
</thead>
<tbody>
#foreach ($pengguna as $pgn)
<tr>
<td>{{ $index++ }}</td>
<td>{{ $pgn->firstname }}</td>
<td>{{ $pgn->lastname }}</td>
<td>{{ $pgn->username }}</td>
<td>{{ $pgn->email }}</td>
<td>{{ $pgn->level }}</td>
<td>{{ $pgn->updated_at }}</td>
</tr>
#endforeach
</tbody>
</table>
....
how to pass the parameter from session to the controller then? so basically pass the {{ Session::get('name')}} to $cid in index controller.
thank you.
hi there i have found the solution, just place refactor the index controller to;
public function index() {
$cid = Session::get('name');
$pengguna = User::where('id_company', '=', $cid)->get();
$pengguna->toarray();
$data = array(
'pengguna' => $pengguna,
'page' => 'master',
'index' => 0
);
return View::make('console.pengguna')->with($data);
}

Categories