[英]Move data from one table with button to another table Laravel
我发现了这个:
但在 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();
假设你有两个表: firsts
和seconds
对于Laravel您必须为这两个表两种型号: First
和Second
分别。
现在,在您的控制器中,
//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>
这可能是执行 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.