Error with my controller and ajax function - php

I used AJAX to create live search function, but I have meet the error on my controller. I think it's syntax error on the last td #method('DELETE'), I don't know how to fix it, can anyone help me ?
public function search(Request $request)
{
$output = '';
$users = User::where('name','LIKE','%'.$request->keyword.'%')->get();
foreach($users as $user)
{
$output += '<tr>
<td>
<div class="d-flex px-2 py-1">
<div>
<img src="'. asset('storage/users/' . $user->image) .'"
class="avatar avatar-sm me-3 border-radius-lg" alt="user1">
</div>
<div class="d-flex flex-column justify-content-center">
<h6 class="mb-0 text-sm">{{ $user->name }}</h6>
<p class="text-xs text-secondary mb-0">'. $user->email .'</p>
</div>
</div>
</td>
<td>
<p class="text-xs font-weight-bold mb-0">'. $user->date_of_birth .'</p>
<p class="text-xs text-secondary mb-0">'. $user->phone .'</p>
</td>
<td class="align-middle text-center">
<span
class="text-secondary text-xs font-weight-bold">'. $user->address .'</span>
</td>
<td class="align-middle">
<form action="" method="POST">
{{ csrf_field() }}
#method('DELETE')
<button class="btn btn-danger" data-toggle="tooltip"
data-original-title="Delete user" type="submit">Delete</button>
</form>
</td>
</tr>';
}
return response()->json($output);
}

One clean solution to achieve what you need is to render the HTML as blade file using view('')->render() instead of building it on the controller
Create a new view file at resources/views/partials/user-search.blade.php and move your HTML to it
#foreach ($users as $user)
<tr>
<td>
<div class="d-flex px-2 py-1">
<div>
<img src="{{ asset('storage/users/'.$user->image) }}"
class="avatar avatar-sm me-3 border-radius-lg" alt="user1">
</div>
<div class="d-flex flex-column justify-content-center">
<h6 class="mb-0 text-sm">{{ $user->name }}</h6>
<p class="text-xs text-secondary mb-0">{{ $user->email }}</p>
</div>
</div>
</td>
<td>
<p class="text-xs font-weight-bold mb-0">{{ $user->date_of_birth }}</p>
<p class="text-xs text-secondary mb-0">{{ $user->phone }}</p>
</td>
<td class="align-middle text-center">
<span class="text-secondary text-xs font-weight-bold">{{ $user->address }}</span>
</td>
<td class="align-middle">
<form action="" method="POST">
{{ csrf_field() }}
#method('DELETE')
<button class="btn btn-danger" data-toggle="tooltip"
data-original-title="Delete user" type="submit">Delete</button>
</form>
</td>
</tr>
#endforeach
then modify your Controller search method to something like:
public function search(Request $request)
{
$users = User::where('name', 'LIKE', '%'.$request->keyword.'%')->get();
$output = view('partials.user-search')->with(['users' => $users])->render();
return response()->json($output);
}

Before I propose my fixes note that #ferhsom solution is a really a cleaner and better way to achieve what you need. But still the error in your code need to be pin pointed.
Errors
First the litteral string
Everything written between single quotes is considered as a string (literally) so no function call or variable will work.
The following string as HTML will give you this: enter image description here:
'
<td>
<form action="" method="POST">
{{ csrf_field() }}
#method("DELETE")
<button type="submit">Delete</button>
</form>
</td>
'
But what you need is hidden inputs with the keys _method and _token.
Second no Blade rendering
#method and #csrf are a Blade directives which will not make any sense if it doesn't pass through the rendering process.
Solution
Replace #method('DELETE') with:
'<input type="hidden" name="_method" value="DELETE">'
For #csrf or {{ csrf_field() }} do:
'<input type="hidden" name="_token" value="' . csrf_token() . '">'
Or
'' . csrf_field() . ''

Related

Laravel 9 "Missing required parameter for [Route:"

I'm using Laravel 9 and trying to use CRUD with my model Project which has the following DB table.
When I try to edit a project using the button "edit" on this view:
"projets.index":
#extends('header')
#section('content')
<div class="col-sm-8" style="background: rgba(204, 204, 204,0.5);padding:5%;width:100%">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2 style="text-align: center">Les projets</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('projets.create') }}"> Créée un nouveau projet</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>id du projet</th>
<th>description</th>
<th width="280px">Action</th>
</tr>
#foreach ($projects as $project)
<tr>
<td>{{ $project->id_project }}</td>
<td>{{ $project->description }}</td>
<td>
<form action="{{ route('projets.destroy',$project->id_project) }}" method="Post">
<a class="btn btn-primary" href="{{ route('galleries.index',$project->id_project,'id_project') }}">ajouter</a>
<a class="btn btn-primary" href="{{ route('projets.edit',[$project->id_project]) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
{!! $projects->links() !!}
</div>
#endsection
I have the following error:
"Missing required parameter for [Route: projets.update] [URI: projets/{projet}] [Missing parameter: projet]."
"projets.edit":
#extends('header')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit projet</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projets.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('projets.update',$project->id_project) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Company Address:</strong>
<input type="text" name="address" value="{{ $project->description }}" class="form-control" placeholder="Company Address">
#error('address')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</form>
#endsection
Update function inside the controller (ProjectController):
public function update(Request $request, $id_project)
{
$request->validate([
'description' => 'required',
]);
User::create($request->all());
$project->description = $request->description;
$project->save();
return redirect()->route('projets.index')
->with('success','project Has Been updated successfully');
}
public function edit(Project $project)
{
return view('projets.edit',compact('project'));
}
Project model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $fillable = [
'id_project', 'description'
];
}
Sorry if my questions seems strange English is not my first language

Too few arguments to function App\Http\Controllers\Home\ClientLogoController::EditClientImage() Controller Laravel

I am using Laravel 9 and I am getting this error I tried various methods to fix this issue but I'm stuck on this . Please help me.
Here is my code:
#php($i=1)
#foreach($client_image as $item)
<tr>
<td>{{ $i++ }}</td>
<td>
<img style="width:60px;height:50px;" src="{{ asset($item->client_logo_images) }}"/>
</td>
<td>
<a href="{{ route('edit.client.logoimage',$item->id) }}">
<i class="glyphicon glyphicon-edit"></i>
</a>
<a class="btn btn-danger sm" href="">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
#endforeach
I have passed Id with the route and capture. Here is the code. :-
Route::get('/edit/client/image/{id}','EditClientImage')->name('edit.client.logoimage');
And this is my controller code .
public function EditClientImage($id) {
$client_logo=ClientLogoImage::findOrFail($id);
return view('admin.home.edit_client',compact('client_logo'));
}
and an error is showing in line 51. This is my line 51. public function EditClientImage($id)
and I am redirecting it to edit_client Here is the code for edit_client 
<form method="post" action="{{ route('update.client.image') }}" enctype="multipart/form-data">
#csrf
<input name="id" type="hidden" value="{{ $client_logo->id }}" >
<div class="card">
<div class="card-header">
<h3 class="card-title">Edit Profile</h3>
</div>
<div class="card-body">
<div class="row">
<div class="form-group">
<label for="formFile" class="form-label mt-0">Profile Image Upload</label>
<input name="client_image" id="image" class="form-control" type="file" >
</div>
</div>
<button class="btn btn-primary" type="submit">Update</button>
</div>
</form>
Thank You
<a href="{{ route('edit.client.logoimage', ['id' => $item->id]) }}">
You need to name it as id...
the problem is with the way you are using name routing:
if you are using routed names you need to pass the parameter the same as the route:
Route::get('/edit/client/image/{id}','EditClientImage')>name('edit.client.logoimage');
in this, you have the id parameter and your link have to be as:
<a href="{{ route('edit.client.logoimage', ['id' => $item->id]) }}">
<i class="glyphicon glyphicon-edit"></i>
</a>
the other way you can use is just use the URL:
<a href="{{ url('/edit/client/image/' . $item->id) }}">
<i class="glyphicon glyphicon-edit"></i>
</a>

How to take index from foreach?

So, I get the problem when I try to make the code for take the store_id from foreach
View
{{-- Cart --}}
<div class="row">
<div class="col-lg-9 my-3">
<div class="table-responsive mb-4">
#foreach ($carts as $cart => $items)
<h3>{{ $cart }}</h3>
{{-- TABLE --}}
<table class="table">
{{-- HEAD --}}
<thead>
<tr>
<th class="border-0 p-3 h6 title" scope="col">
Product
</th>
<th class="border-0 p-3 h6 title" scope="col">
Price
</th>
<th class="border-0 p-3 h6 title ps-5" scope="col">
Quantity
</th>
<th class="border-0 p-3 ps-4 h6 title" scope="col">
Total
</th>
</tr>
</thead>
{{-- CONTENT --}}
<tbody class="border-0">
#foreach ($items as $cart)
<tr>
<td class="p-3 border-0">
<div class="d-flex align-items-center">
<a class="reset-anchor d-block animsition-link" href="/products/{{ $cart->product->slug }}">
#if($cart->product->image)
<img src="img/admin_store/{{ $cart->product->image }}" width="70" />
#else
<img src="{{ asset('img/customer/img-1.png') }}" width="70" />
#endif
</a>
<div class="ms-3">
<a class="reset-anchor animsition-link title text-decoration-none" href="/products/{{ $cart->product->slug }}">{{ $cart->product->name }}</a>
</div>
</div>
</td>
<td class="p-3 align-middle border-0">
<p class="mb-0 small">Rp{{ number_format(($cart->product->price * ((100 - $cart->product->discount)/100)), 0,",",".") }}</p>
</td>
<td class="p-3 align-middle border-0">
<form action="/update_cart" method="POST">
#csrf
<div class="row">
<div class="col-5">
<input type="hidden" name="cart" value="{{ $cart->id }}">
<input type="number" name="quantity" value="{{ $cart->qty }}" class="w-100 ms-5" min="0" max="{{ $cart->product->stock }}">
</div>
</div>
<button type="submit" class="btn btn-sm btn-dark ms-5 mt-2" style="margin-right:20px">Update</button>
</form>
</td>
<td class="p-3 align-middle border-0">
<p class="mb-0 small">Rp{{ number_format($cart->total_product, 0,",",".") }}</p>
</td>
<td class="p-3 align-middle border-0">
<form action="/delete_cart" method="post">
#csrf
<input type="hidden" name="id" id="id" value="{{ $cart->id }}">
<a class="reset-anchor">
<button type="submit" class="btn btn-link">
<img src="{{ asset('img/customer/bx-trash.svg') }}" width="20">
</button>
</a>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
<hr>
{{-- TOTAL --}}
<div>
<h6 class="text-md-end">Total : Rp{{ $items->sum('total_product') }}</h6>
</div>
{{-- SHIPPING --}}
<div class="text-md-end">
<a class="btn btn-dark" href="/shipping/{{ $items->store_id }}"> checkout <i class='bx bx-basket'> </i> </a>
</div>
#endforeach
</div>
</div>
so in <a class="btn btn-dark" href="/shipping/{{ $items->store_id }}"> checkout <i class='bx bx-basket'> </i> </a> this is error
The Controller
public function index(Request $request)
{
$itemuser = $request->user();
$cartdetail = CartDetail::where('user_id', $itemuser->id)->get();
$cart = Cart::where('user_id', $itemuser->id)->get();
return view('customer.cart', [
'title' => 'Cart',
'carts' => $cart,
'cartdetail' => $cartdetail
]);
}
this result when using dd($carts)
This is my view
*note : store name, product and pictures are for study only, and the url before I add $items->store_id
So the story is, I'm only going to checkout for carts with the same store_id, but I'm confused about how to pull the store_id. How to solve it?

"Class App\Http\Controllers\Frontend\FrontendController does not exist"

I installed a laravel script. but a page not worked and appear
Trying to get property 'name' of non-object (View:
/mylocal/core/resources/views/admin/users/servicePrice.blade.php)
for this line:
<th>{{ $item->service->name }}</th>
this is the file codes:
<div class="tile">
#include('admin.layouts.flash')
<h3 class="tile-title">Service Price Details</h3>
<form method="post" action="{{ route('store.service.price', $user_id) }}">
#csrf
#method('PUT')
<table class="table table-hover">
<tbody>
#foreach($items as $item)
<tr>
<th>{{ $item->category->name }}</th>
<th>{{ $item->service->name }}</th>
<td>
<div class="input-group">
<input type="hidden" name="id[]" value="{{ $item->id }}">
<input class="form-control" type="text" name="price[]" value="{{ $item->price }}">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</td>
</tr>
#endforeach
</table>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
Check your relation return a object or not.
<th>
#if($item->service)
{{ $item->service->name }}
#endif
</th>
You didn't share the content of array $items.Please make sure you have a service array in $items array and also make sure the name variable exists in service array.
and can you show me the result of this:
var_dump($items);

Export to excel a table from laravel

I'm so confused why i can't export this to excel. Please help me. Theres an error where it can't read "all".
Here's my code in my UsersController
public function userExport()
{
Excel::create('data_function',function($excel){
$excel->sheet('mysheet', function($sheet){
$sheet->loadView('user.list');
});
})->download('xls');
}
Then here is it in my user/list.blade
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Employees
<small>#lang('app.list_of_registered_users') - {{ $all }}</small>
</h1>
</div>
</div>
#include('partials.messages')
<form method="GET" action="" accept-charset="UTF-8" id="users-form">
<div class="row tab-search">
<div class="col-md-2">
<div class="form-group-sm">
<select class="form-control form-control-sm" id="company" name="company">
<option selected disabled>Company</option>
#foreach ($companies as $company)
#if (($company->id)=="1")
<option>All</option>
#else
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endif
#endforeach
</select>
</div>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-warning btn-sm btn-block" id="filter-user">
<i class="glyphicon glyphicon-filter"></i>
Filter Employee
</button>
</div>
<div class="col-md-1">
<a href="{{ route('user.export') }}" class="btn btn-sm btn-success btn-block">
<i class="fa fa-file-excel-o"></i>
Excel
</a>
</div>
<div class="col-md-2">
<a href="{{ route('user.create') }}" class="btn btn-primary btn-sm btn-block" id="add-user">
<i class="glyphicon glyphicon-plus"></i>
Add
Employee
</a>
</div>
</div>
<div class="row tab-search"> <div class="col-md-2">
<div class="form-group-sm">
<select class="form-control form-control-sm" id="benefit" name="benefit">
<option selected disabled>Benefits</option>
#foreach ($benefits as $benefit)
#if (($benefit->id)=="1")
<option>All</option>
#else
<option value="{{ $benefit->id }}">{{ $benefit->name }}</option>
#endif
#endforeach
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group-sm">
<div class="input-group custom-search-form-sm">
<input type="text" class="form-control form-control-sm" name="search" value="{{ Input::get('search') }}" placeholder="Search Name">
<span class="input-group-btn">
<button class="btn btn-default btn-sm" type="submit" id="search-users-btn">
<span class="glyphicon glyphicon-search"></span>
</button>
#if (Input::has('search') && Input::get('search') != '')
<a href="{{ route('user.list') }}" class="btn btn-danger btn-sm" type="button" >
<span class="glyphicon glyphicon-remove"></span>
</a>
#endif
</span>
</div>
</div>
</div>
</div>
</div>
</form>
<div class="table-responsive" id="users-table-wrapper">
<table class="table table-bordered table-striped table-hover table-condensed" id="datatable-default">
<thead>
<th>#lang('app.full_name')</th>
<th>Sex<i type="button" class="fa fa-fw fa-sort"></th>
<th>Birthday<button name="birthday_sort" style="background-color:#ffffff"><i class="fa fa-fw fa-sort"></button></th>
<th>Age<i class="fa fa-fw fa-sort"></th>
<th>Civil<br>Status<i class="fa fa-fw fa-sort"></th>
<th>Company<i class="fa fa-fw fa-sort"></th>
<th>Department<i class="fa fa-fw fa-sort"></th>
<th>Employee<br>Status<i class="fa fa-fw fa-sort"></th>
<th>Level<i class="fa fa-fw fa-sort"></th>
<th>Date<br>Hired<i class="fa fa-fw fa-sort"></th>
<th>Tenure</th>
</thead>
<tbody>
#if (count($users))
#foreach ($users as $user)
<tr class="gradeX">
<td>{{ $user->first_name . ' ' . $user->middle_name . ' ' . $user->last_name }}</td>
<td>{{ $user->gender[0] }}</td>
<td>{{ $user->birthday->format('M j/y') }}</td>
<td>{{ $user->age }}</td>
<td>{{ $user->civil_status }}</td>
<td>#foreach ($user->company as $company)#endforeach{{ $company->name }}</td>
<td>#foreach ($user->department as $department)#endforeach{{ $department->name }}</td>
<td>{{ $user->emp_status }}</td>
<td>{{ $user->level }}</td>
<td>{{ $user->date_hired }}</td>
<td>{{ $user->difference }}</td>
</tr>
#endforeach
#else
<tr class="gradeX">
<td colspan="6"><em>#lang('app.no_records_found')</em></td>
</tr>
#endif
</tbody>
</table>
{!! $users->render() !!}
</div>
The problem is that it says an error like "all","companies", and etc are undefined variables. Also because the companies is another table. How can define it in my excel that it won't be error again?
It seems you need to pass variables to view from controller which you are passing. Please check my code and update.
public function userExport()
{
$companies = $this->companyModel->get();
$all = $this->something->get();
Excel::create('data_function',function($excel) use($all, $companies) {
$excel->sheet('mysheet', function($sheet) use($all, $companies) {
$sheet->loadView('user.list', ['all' => $all, 'companies' => $companies]);
});
})->download('xls');
}
and also pass the other variables with same as I passed to $sheet->loadView() in example.
I think this will help you.

Categories