【问题标题】:Move data from one table with button to another table Laravel使用按钮将数据从一个表移动到另一个表 Laravel
【发布时间】:2018-11-03 07:20:52
【问题描述】:

我找到了这个:

Move data from one MySQL table to another

但是在 Laravel 中它有点不同。像他一样,我想要一个按钮,它可以删除像这样的表中的一行: (更新图片)

举个例子。在他点击按钮后,它应该将显示的行移动到数据库中,就像这里显示的一样,然后将其删除。我真的不知道如何在 Laravel 中开始这样的事情,我真的找不到相关的东西。

也许这样会更清楚:

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

【问题讨论】:

  • 点击按钮后,它应该将显示的行移动到数据库中,就像这里显示的一样,然后将其删除。。那么您的问题是关于如何删除数据、如何移动数据或如何将其移动到备份然后删除原始数据?你的问题很模糊,你能详细说明一下吗?例如,您已经尝试过什么?哪里失败了?
  • 我的问题是如何将数据移动到另一个表,然后删除显示为图片的原始数据。
  • 你的意思是在数据库表之间移动还是在 HTML 表之间移动?其次,您尝试过什么?
  • @Devi 在图片中显示了 HTML 表格的一行,所以不是很清楚;)
  • 我已经编辑了我的帖子,现在您可以看到内连接,它是所示表格的核心......

标签: php laravel


【解决方案1】:

这可能是执行 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

    }

}

【讨论】:

    【解决方案2】:

    假设您有两个表:firstsseconds 对于 Laravel,这两个表必须有两个模型:分别为 FirstSecond

    现在,在您的控制器中,

    //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);
    
    }
    

    请记住,上面的控制器函数是在单击按钮时调用的,并且必须传递一个 id

    路线将是这样的:

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

    而且,您的按钮链接如下:

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

    【讨论】:

    • 上面显示的我的表只是一个内部连接。所以我有表借出加入表客户。结果就是上图。您在此处共享的代码是否仍然可行?如果你愿意,我可以向你展示内部连接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多