Move data from one table with button to another table Laravel

ε祈祈猫儿з 提交于 2020-01-15 05:25:07

问题


Before someone say this is a duplicate. Yes i have found this:

Move data from one MySQL table to another

But in Laravel its a bit different this is quiet that what i want. Like him i want a button wich delete a row in a tabel like this one: (Updated picture)

Just to have an example. After he hit the button it should move the shown row into the database just like it is shown here and delete it afterwards. I really dont know how to start something like this in laravel and i really cant find something related so if you need code snippets from what i have just tell me what you need. I appreciate every help thank you.

EDIT

Maybe this will make it more clearly:

$user_input = $request->userInput
$scores = DB::table('cd')
->join('customers', 'cd.fk_lend_id', '=', 'customer .lend_id')
->select('cd.fk_lend_id','cd.serialnumber','users.name', 'cd.created_at as lend on')
->where('cd.fk_lend_id',$request->$user_input)
->get();

回答1:


Suppose you have two tables: firsts and seconds For Laravel you must have two Models for these two tables: First and Second respectively.

Now, in your controller,

//import your models
use App\First;
use App\Second;

//create a function which takes the id of the first table as a parameter
public function test($id)
{
    $first = First::where('id', $id)->first(); //this will select the row with the given id

    //now save the data in the variables;
    $sn = $first->serialnumber;
    $cust = $first->customer;
    $lendon = $first->lend_on;
    $first->delete();

    $second = new Second();
    $second->serialnumber = $sn;
    $second->customer = $cust;
    $second->lend_on = $lendon;
    $second->save();

    //then return to your view or whatever you want to do
    return view('someview);

}

Remember the above controller function is called on button clicked and an id must be passed.

The route will be something like this:

Route::get('/{id}', [
    'as' => 'test',
    'uses' => 'YourController@test',
]);

And, your button link like:

<a href="{{ route('test',$id) }}">Button</a>



来源:https://stackoverflow.com/questions/50502989/move-data-from-one-table-with-button-to-another-table-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!