Laravel 5:一个更新2个链接表的表单。怎么样?

时间:2015-05-06 14:05:34

标签: php forms laravel

我有两个简单的表格:

CUSTOMERS
id, email

CLAIMS
id, customer_id(fk), description

我创建了相关模型(Customers.phpClaims.php)以及设置关系:hasOne()belongsTo()

我也准备好相关的RESTful控制器:CustomersController.phpClaimsController.php

如果我需要通过提交一个表单在两个表中创建/更新记录,那么最佳解决方案是什么?创建一个通用控制器?混合型号?

我一直在Laravel文档和Google上搜索,但仍然不知道如何实现这一目标。

1 个答案:

答案 0 :(得分:0)

客户模式

public function claims(){
    return $this->hasMany('App\Claims');
}

索赔模型

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

现在在控制器中你需要在商店行动中发送请求

像这样的东西

class CreateCustomerClaim extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required',
            'description'=> 'required'

        ];
    }
}

现在在商店操作中发送您的请求,从请求中获取数据并将其插入db

public function store(CreateCustomerClainsRextends $request)
{
    //example
    $customer= new Customer($request->all());

    Auth::user()->claims()->save($customer);

}

如果你需要更新在更新功能中使用相同的请求,当你从请求中获取数据时只需使用更新。这是我从一个请求更新3个不同表的示例

public function update($id,ArtikalUpdateRequest $request)
{
    $article = Artikal::findOrFail($id);
    if($article !== null){

        $article->update($request->all());

        \DB::table('artikal_podkategorija')
            ->where('artikal_id', $article->id)
            ->update(array('podkategorija_id' => $request['podkategorija']));

        \DB::table('arikalslike')
            ->where('artikal_id', $article->id)
            ->update(array('NazivSlike' => $request['NazivSlike']));

        $slika =   \DB::table('arikalslike')
            ->where('artikal_id', $article->id)->first();



        $image = Request::file('image');
        //dd($image);

        if($image != null){

            $destinationPath = 'uploads/artiklislike/';


            $thumb = $slika->SifraSlike;
            $fileName = $thumb;
            $nazivthumb =  $slika->NazivThumb;


            $slika->NazivSlike = $request['NazivSlike'];
            $slika->NazivThumb =  $nazivthumb;
            $slika->SifraSlike = $fileName;
            $slika->artikal_id = $article->id;


            //Snima sliku

            $img = Image::make(Input::file('image'));
            $destinationPath = $destinationPath.$fileName;
            Image::make($img)->save($destinationPath);

            // Snima sliku u manjem formatu thumb

            $destinationPath = 'uploads/artiklislike/';
            $img = Image::make(Input::file('image'));
            $destinationPath = $destinationPath.$nazivthumb;
            Image::make($img)->resize(300, 200)->save($destinationPath);
        }
    }

    return redirect('artikli')->with(['flash_message' => 'Uspiješno ste obrisali artikal!']);
}