I can't use onlyTrashed method on datapanel in laravel - laravel

I have a problem with Laravel datatable SoftDelete method in Laravel. I use onlyTrashed() function to get the data but all data is coming.I am using the latest version yajrabox.
Frist try:
public function trash(Request $request){
$datalists = DataTables::of(Datapanel::onlyTrashed())->onlyTrashed()->make(true);
return view('back.cancelpanel');
}
Second try:
public function trash(Request $request){
if ($request->ajax()) {
return Datatables::of(Datapanel::onlyTrashed())
->onlyTrashed()
->make(true);
}
return view('back.cancelpanel');
}
Third try:(In this try i reached the data deleted with json data but I didn't know how to direct it to datatable.)
public function trash(Request $request){
$datalists = DataTables::of(Datapanel::onlyTrashed())->make(true);
return response()->json($datalists);
}

Related

Can I use return to a public function in controller in Laravel 8

I got an error which is undefined function.
I tried to use a public function in return:
public function CreateForm() //This the function that I want to use back
{
$names2 = DB::table('pendaftaran')
->where('isActive',0)
->orderBy('id','desc')
->get();
return view('contact')->with($variables);
}
So this a function I want to return to a function
public function AddUserSubmit(Request $request)
{
$this->validate($request,
[
'Nama'=>'required',
'NoKP'=>'required',
]);
Pendaftaran::create($request->all());
return CreateForm(); //Can I return to a public function ?
}
Yes this should work but you need to add context and use the $this keyword otherwise return CreateForm(); will be interpreted as trying to invoke a global function.
Try replacing:
return CreateForm();
With
return $this->CreateForm();
See this other question for more information: https://stackoverflow.com/a/17861505/4517964

accessing custom method in model

Im practicing laravel and im making a custom method for my user
In my user model i have build a function like this
public function employee(){
return $this->where('user_type','employee');
}
and then in my controller I'm accessing the function like this
public function index(){
$users = User::latest()->employee();
return UserResource::collection($users);
}
but it return an error Method Illuminate\Database\Query\Builder::employee does not exist.how to fix this?
Use local scope instand
public function scopeEmployee($query)
{
return $query->where('user_type', 'employee');
}
Your controller can be as it was !
public function index(){
$users = User::latest()->employee()->get();
return ProductsResource::collection($users);
}

How to print Last Executed Query on controller in Laravel 5.4?

I have created following method to verify admin in
AdminModel.php
public function verifyAdmin($where){
$admin = AdminModel::where($where)->get();
if($admin->isEmpty()){
$emptyArray=array();
return $emptyArray;
}else{
return $admin;
}
}
Now this model's method is used in controller as follows AdminController.php
public function adminLogin(AdminLoginRequest $request)
{
$adminModel = new AdminModel();
$email=$request->email;
$password=$request->password;
$where=array('email'=>$email,'password'=>md5($password));
$getUser=$adminModel->verifyAdmin($where);
if(!empty($getUser)){
session()->put('email',$email);
return redirect('admin-dashboard');
}else{
return redirect('admin')->with('error', 'Invalid email or password !');
}
}
Now I want to echo last executed query in the controller after the models method call .
How can I do this please suggest me, I am using Laravel 5.4

How to redirect store to update method?

How to redirect store to update method? I tryed the following code:
public function store(ProductRequest $request)
{
return $this->update(new Product, $request);
}
public function update(Product $product, ProductRequest $request)
{
// code
}
However, the first parameter of update need an already in database user and the above code does not work as expected. (it update the entire users in db!)
What is the correct way to achieve that?
public function store(UserRequest $request)
{
return $this->maintain(new User, $request);
}
public function update(User $user, UserRequest $request)
{
return $this->maintain($user, $request);
}
private function maintain($user, $request)
{
//code;
}
The model for the update method could be the problem, your code is okay for this part:
public function store(Request $request)
{
return $this->update(new Product, $request);
}
public function update(Product $product, Request $request)
{
$product->fill($request->all())->save();
// code
}
For example, with route model binding:
Route::resource('products', 'ProductController');
Route::model('products', App\Product::class);
Or with a custom binding:
Route::resource('products', 'ProductController');
Route::bind('products', function($param) {
return Product::where('slug', $param)->first();
});
Make sure you are not using get() in custom binding, it will pass back a collection.

How to pass argument in Laravel 5, using html tag?

Hi, am new to laravel5, In the HTML code, I need to call this below destroy function. I'd written this in Controller page.
Could anyone help me?
public function destroy($id)
{
Book::find($id)->delete();
return Redirect::route('books.index')->withInput();
}
You are trying to return $input (withInput) but it is not available because you did not create it.
It seems that you want to return a record that is already deleted? If you want to return the deleted data you can do this:
public function destroy($id)
{
// First get the data
$data = Book::find($id);
// Then delete it
Book::find($id)->delete();
// Finally return it
return Redirect::route('books.index')->withData();
}
Or you can try it in one line (not sure this works):
public function destroy($id)
{
$data = Book::find($id)->delete();
return Redirect::route('books.index')->withData();
}

Resources