php - 在 Laravel 5 中创建帐户时,如何在数据库中为用户创建现有的表列

标签 php mysql laravel laravel-5

我创建了一个非常简单的应用程序,用户可以在其中更新存储在数据库中的“金额”。一切正常,但我的问题是现在用户必须在数据库中创建一个实例,然后他们才能更新数据库中现有实例的“金额”列。

我希望拥有它,以便“金额”字段已经存在并在创建帐户时连接到该特定用户。用户不必为“金额”创建一个实例,然后就可以更新它。

此外,我还使用“Laravel Collective”来显示“Forms & HTML”在index.php中

我希望有人能给我指出正确的方向来开始这个工作,因为我对这个过程如何运作有点一无所知。我从哪说起呢?

这是我的index.blade.php 中的按钮和表单

            {{--  This is to update the amount  --}}
            @if(count($amounts) > 0)
            @foreach($amounts as $total)
            {!! Form::open(['action' => ['CurrencyBlocksController@update', $total->id], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
                <div class="form-group">
                    <h3>{{Form::label('amount', 'Amount:')}}</h3>
                    {{Form::text('amount', $total->amount, ['class' => 'form-control', 'placeholder' => 'Title'])}}
                </div>
                {{Form::hidden('_method', 'PUT')}}
                {{Form::submit('Submit', ['class' => 'btn btn-primary'])}}    
            {!! Form::close() !!}
            @endforeach
            @endif

我如何设置网络路由:

Route::resource('currency_blocks', 'CurrencyBlocksController');
Auth::routes();

这是数据库中表的快照:

enter image description here

这是 Controller [唯一重要的是index()和update()函数]:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\User;
use App\CurrencyBlock;

class CurrencyBlocksController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        $amounts = CurrencyBlock::find('amount');
        return view('blocks.index')->with('amounts', $user->amounts);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'amount' => 'required'
        ]);

        // Update Post
        $total = CurrencyBlock::find($id);
        $total->amount = $request->input('amount');
        $total->user_id = auth()->user()->id;
        $total->save();

        //return redirect('/currency_blocks/{{$amount->id}}/edit')->with('amount', $amount)->with('success', 'Post Updated');
        return redirect('/currency_blocks')->with('success', 'Amount Updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

货币区 block :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CurrencyBlock extends Model
{
    // Table Name
    protected $table = 'currency_blocks';

    // Primary Key
    public $primaryKey = 'id';

    public function user(){
        return $this->belongsTo('App\User');
    }
}

最佳答案

您可以在创建用户时创建条目,如下所示

$user = User::create($data); 
$user->currency()->create(here you put ur empty entry of currency );

并且不要忘记在用户模型中添加货币关系;

public function currency () {return $this->has one(User::class,'user_id');}

关于php - 在 Laravel 5 中创建帐户时,如何在数据库中为用户创建现有的表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48132992/

相关文章:

php - 如何通过从我的数据库中读取值来选择单选按钮?

php - Cakephp 3 - 从数据库中减去一个值

MySQL UUID_SHORT() 给出错误 Out of range value for column

linux - Composer在您的平台中检测到问题: Your Composer dependencies require a PHP version ">= 7.3.0"

php - 如何将列数据转换为行数据

php - 无法使用 PHP 根据要求格式化日期

java - oneToMany 连接时如何避免持久化新对象?

c# - MySQL 错误 2003 : Can't connect to MySQL server on 'localhost'

javascript - 使用 npm 将 tokenfield 包包含到 laravel

php - Laravel 5.2.x 禁用特定的中间件