John Davidson

php - Can you output a button via controller in Laravel?

0 comments
Message:


I have a live search via my controller method and it outputs the results but I want to have a button for editing present too


what I have tried:


function action(Request $request)
{
if ($request->ajax()) {
$output = '';
$query = $request->get('query');
if ($query != '') {
$data = DB::table('customers')
->where('id', 'like', '%' . $query . '%')
->orWhere('email', 'like', '%' . $query . '%')
->orWhere('name', 'like', '%' . $query . '%')
->orderBy('id', 'desc')
->get();

} else {
$data = DB::table('customers')
->orderBy('is', 'desc')
->get();
}

$total_row = $data->count();
if ($total_row > 0) {
foreach ($data as $row) {
$output .= '
<tr>
<td>' . $row->id . '</td>
<td>' . $row->email . '</td>
<td><a href="{{url("cust-edit/".$data->id)}}" class="btn btn-success">Edit</a></td>
</tr>
';
}
} else {
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}

$data = array(
'table_data' => $output,
'total_data' => $total_row
);

echo json_encode($data);
}
}

Edit: Added the full method.
Note. this live search should not refresh the page, rather replace values



My Answer:
>ajax()) {
$data = // get search results
$output = '';
foreach ($data as $item) {
$output .= '
'.$item->name.'
';
$output .= '';
}
return response()->json(['output' => $output]);
}
}

This code will generate a button for each search result item that says "Edit".

Rate this post

4 of 5 based on 3407 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us