php—在Laravel5中创建帐户时,如何在数据库中为用户提供一个现有的表列

iyfjxgzm  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(265)

我创建了一个非常简单的应用程序,用户可以在其中更新存储在数据库中的“数量”。一切都正常工作,但我的问题是,现在一个用户必须在数据库中创建一个示例,然后他们才能更新数据库中现有的示例“amount”列。
我想有它,使'金额'字段已经存在,并连接到特定的用户时,他们创建一个帐户。而不是用户必须为“amount”创建一个示例,然后他们才能更新它。
我还使用了一个“laravelcollective”来表示“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

如何设置web路由:

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

以下是数据库中表的快照:

下面是控制器[唯一重要的是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)
    {
        //
    }
}

当前块:

<?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');
    }
}
eiee3dmh

eiee3dmh1#

您可以在这样创建用户时创建条目

$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');}

相关问题