简体   繁体   English

将数据从一个带有按钮的表移动到另一个表 Laravel

[英]Move data from one table with button to another table Laravel

I have found this:我发现了这个:

Move data from one MySQL table to another 将数据从一个 MySQL 表移动到另一个

But in Laravel it's a bit different.但在 Laravel 中有点不同。 Like him I want a button which deletes a row in a table 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 don't know how to start something like this in Laravel and I really can't find something related.我真的不知道如何在 Laravel 中开始这样的事情,我真的找不到相关的东西。

Maybe this will make it more clear:也许这会让它更清楚:

$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();

Suppose you have two tables: firsts and seconds For Laravel you must have two Models for these two tables: First and Second respectively.假设你有两个表: firstsseconds对于Laravel您必须为这两个表两种型号: FirstSecond分别。

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.记住上面的控制器函数是在点击按钮时调用的,并且必须传递一个id

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>

This might be a simpler way to do the Laravel "move record" part of this.这可能是执行 Laravel “移动记录”部分的一种更简单的方法。

use App\Http\Controllers\Controller;
use App\Models\TableOne;
use App\Models\TableTwo;
use Illuminate\Http\Request;

class MoveOneRecord extends Controller
{

    public static function move_one_record(Request $request)
    {

        // before code

        // get id
        $id = intval( $request->input('id', 0) );

        // grab the first row of data
        $row_object = TableOne::where('id', $id))->first();

        // check if we have data
        if (empty($row_object)) { throw new Exception("No valid row data."); }

        // convert to array
        $row_array = $row_object->toArray();

        // unset the row id (assuming id autoincrements)
        unset($row_array['id']);

        // insert the row data into the new table (assuming all fields are the same)
        TableTwo::insert($row_array);

        // after code

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM