0

I am working on a project of mine, where I want to get a table called 'Clients' from a database and display its data. All good, this was an easy task. Now I want to update some data in the table. I am trying to figure out which is the optimal way of doing this having in mind that I am working with Laravel framework.

I have the following for loop that I display the data in a .

for ($i=0; $i<$clientNumber; $i++) { ?>
    <tr>
    <td>
        <form method="post" action="{{URL::to('/')}}">
            {{csrf_field()}} <!-- Used for the token --> 
            <button type="submit" value="<?php echo $i; ?>" class="editButton" > <i class="fa fa-edit"></i> </button>
        </form>
    </td>
    <td> <?php echo $data[$i]->clientId; ?> </td>
    <td> <?php echo $data[$i]->clientFirstname; ?> </td>
    <td> <?php echo $data[$i]->clientSurname; ?> </td>
    <td> <?php echo $data[$i]->clientEmail; ?> </td>
    <td> <?php echo $data[$i]->clientMobile; ?> </td>
    <td> <?php echo $data[$i]->clientPhone; ?> </td>
    <td> <?php echo $data[$i]->clientAdrress; ?> </td>
    <td> <?php echo $data[$i]->companyName; ?> </td>
    <td> <?php echo $data[$i]->companyType; ?> </td>
    <td> <?php echo $data[$i]->services; ?> </td>
    <td> <?php echo $data[$i]->websiteURL; ?> </td>
    <td> <?php echo $data[$i]->renewDate; ?> </td>
    <td> <?php echo $data[$i]->totalPrice; ?> </td>
    <td> <?php echo $data[$i]->deposit; ?> </td>
    <td> <?php echo $data[$i]->balance; ?> </td>
    <td> <?php echo $data[$i]->serverPrice; ?> </td>
    <td> <?php echo $data[$i]->comments; ?> </td>
    <input type="hidden" name="rowId" value="<?php echo $i; ?>">
    </tr> 
<?php
}

As you can see I have added the following code

<button type="submit" value="<?php echo $i; ?>" class="editButton" > <i class="fa fa-edit"></i> </button>

so that I can have a button for each row and when I click that I will be prompted to an edit area.

I would like to solve this without loading another page. For example I already developed a version with a <form> that when the button is clicked I am redirected to the new page.

I am struggling because I don't know how to form an SQL query in Laravel that depend on the row number (which will be known after a button or something similar is pressed). For example SELECT * FROM table WHERE id==XXX.

Is the problem clear enough?

Thanks for your time, -Vasilis

4
  • 1
    No the question is not clear. What should happen when you click the button? You want something to happen without the normal form submission, but also without Javascript? Is the SQL query a second part of the question? Commented Dec 2, 2018 at 18:36
  • Oh, when I click the button I want to update some data to the database. I need to run a simple UPDATE query. I dont want to reload the page or send form data to another php file. Anyway which is the best way of doing that in Laravel? Commented Dec 2, 2018 at 20:19
  • I think probably you have some terminology mixups. Your title says you want to avoid a form and Javascript - you need a form if you want your user to input data. You say you don't want to send data to another PHP file - you are going to have to do that if you want to do anything with that data. You say you want to avoid JS, but you are going to have to use JS if you don't want to do a normal form submission. I'd suggest reading through the Laravel docs, they are very good and it is quite straightforward, or find a basic Laravel CRUD tutorial. Commented Dec 3, 2018 at 10:22
  • The Laravel 5.2 docs included a basic tutorial which I found very helpful, though sadly it doesn't seem to exist for later versions. Some details may have changed but for the approach and outline are the same. Commented Dec 3, 2018 at 10:36

1 Answer 1

1

I want to update some data in the table... without loading another page... to form an SQL query in Laravel that depend on the row number....

I highly recommend you take a spin through Laravels blade documentation.

*Edited to use Route Model Binding

YourController:

use App/Client;

public function update(Request $request, Client $client)
{
    $client->update($request->all());

    // Then, return back to the view 
    return back()->with('success', 'Field updated successfully!');
}

Blade:

<!-- optional -->
@if (session('success'))
    <div class="alert alert-success" role="alert">
        {{ session('success') }}
     </div>
@endif

<form method="POST" action="/your/route/{{ $i }}">
    @csrf 
    @method('PUT')
    <!-- form inputs -->

    <button type="submit" class="editButton">Edit</button>
</form>

Your web routes file:

Route::put('/your/route/{client}', 'YourController@update')->name('update-client');
Sign up to request clarification or add additional context in comments.

8 Comments

So, at your opinion you think the better way of implementing this is using a new file that does the update query?
While this is generally the right approach, the specifics are not good practice and don't take advantage of Laravel. Eg on the form, use a REST endpoint for the action, and use Form method spoofing. In your controller, use type hinting and Model Binding to access the model you are updating. Use Eloquent to update your model. This sounds complicated but find a CRUD tutorial and walk through it, it is all straightforward.
@Anonymous what do you mean using a new file? You have a controller already - I just called it YourController to avoid confusion. This will do what I think you're hoping to accomplish.
@Don'tPanic I agree it's a simplified version. Judging from the OP's question I think there's some considerable gaps in their understanding of Laravel / PHP in general. Explaining REST, Route Model Binding, Eloquent, etc, are well outside the scope of the original question.
@Don'tPanic That said, I edited my answer to reflect your comments
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.