php无限制字段属性,php – 动态且无限制地添加laravel中的字段

我有我的网站的一部分,用户或管理员可以添加餐厅列表(真的像帖子,只是不同的命名)

有一些固定的输入,如(标题,描述和地图)我还需要一个部分,用户/管理员可以添加餐厅菜单这个选项显然可以是不同的每个餐厅,因为他们的菜单是一个短列表或长列表.

所以我需要的就是按钮,人们可以在其中添加字段,并使用另一个字段命名他们的菜单项以获得每个项目的价格.

所以我的问题是如何实现这个选项?

我现在有什么?

餐厅迁移:

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateRestaurantsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('restaurants', function (Blueprint $table) {

$table->increments('id');

$table->string('title')->unique();

$table->string('slug')->unique();

$table->string('description')->nullable();

$table->string('image')->nullable();

$table->string('menu')->nullable();

$table->string('address')->nullable();

$table->integer('worktimes_id')->unsigned();

$table->integer('workday_id')->unsigned();

$table->integer('user_id')->unsigned();

$table->string('verified')->default(0);

$table->string('status')->default(0);

$table->timestamps();

});

Schema::table('restaurants', function($table) {

$table->foreign('worktimes_id')->references('id')->on('worktimes');

$table->foreign('workday_id')->references('id')->on('workdays');

$table->foreign('user_id')->references('id')->on('users');

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('restaurants');

}

}

这就是全部,我仍然没有为餐厅创建CRUD控制器,因为我正在坚持这个选项和你的意见.

谢谢.

UPDATE

商店方法:

public function store(Request $request)

{

//Validating title and body field

$this->validate($request, array(

'title'=>'required|max:225',

'slug' =>'required|max:255',

'image' =>'sometimes|image',

'description' => 'required|max:100000',

'address' => 'sometimes|max:500',

'user_id' => 'required|numeric',

'verified' => 'sometimes',

'status' => 'required|numeric',

));

$restaurant = new Restaurant;

$restaurant->title = $request->input('title');

$restaurant->slug = $request->input('slug');

$restaurant->description = $request->input('description');

$restaurant->address = $request->input('address');

$restaurant->user_id = $request->input('user_id');

$restaurant->verified = $request->input('verified');

$restaurant->status = $request->input('status');

if ($request->hasFile('image')) {

$image = $request->file('image');

$filename = 'restaurant' . '-' . time() . '.' . $image->getClientOriginalExtension();

$location = public_path('images/');

$request->file('image')->move($location, $filename);

$restaurant->image = $filename;

}

// menu

$newArray = array();

$menuArray = $request->custom_menu; //Contains an array of Menu Values

$priceArray = $request->custom_price; //Contains an array of Price Values

//Creating new array with ARRAY KEY : MENU VALUES and ARRAY VALUE: PRICE VALUES

foreach ($menuArray as $key => $singleMenu) {

$newArray[$singleMenu] = $priceArray[$key];

}

//Output : array("Menu01" => "Price01", "Menu02" => "Price 02", "Menu03" => "Price 04", "Menu04" => "Price 05")

//Converting array to json format to store in your table row 'custom_menu_price'

$jsonFormatData = json_encode($newArray);

//Output like: {"Menu01":"Price01","Menu02":"Price 02","Menu03":"Price 04","Menu04":"Price 05"}

// Save in DB

//

//

//

// To retrieve back from DB to MENU and PRICE values as ARRAY

$CustomArray = json_decode($jsonFormatData, TRUE);

foreach ($CustomArray as $menu => $price) {

echo "Menu:".$menu."
";

echo "Price:".$price."
";

}

// menu

$restaurant->save();

$restaurant->workdays()->sync($request->workdays, false);

$restaurant->worktimes()->sync($request->worktimes, false);

//Display a successful message upon save

Session::flash('flash_message', 'Restaurant, '. $restaurant->title.' created');

return redirect()->route('restaurants.index');

解决方法:

你能做的是

1)在迁移文件中为custom_menu_price添加另一个表行

$table->string('custom_menu_price')->nullable();

2)修改您的表单

{{ csrf_field() }}

//I'm Looping the input fields 5 times here

@for($i=0; $i<5; $i++)

Enter Menu {{ $i }} : //**Assign name as ARRAY

Enter Price {{ $i }} : //**Assign name as ARRAY

@endfor

3)在你的控制器中

public function store(Request $request) {

//Validating title and body field

$this->validate($request, array(

'title'=>'required|max:225',

'slug' =>'required|max:255',

'image' =>'sometimes|image',

'description' => 'required|max:100000',

'address' => 'sometimes|max:500',

'user_id' => 'required|numeric',

'verified' => 'sometimes',

'status' => 'required|numeric',

));

$restaurant = new Restaurant;

$restaurant->title = $request->input('title');

$restaurant->slug = $request->input('slug');

$restaurant->description = $request->input('description');

$restaurant->address = $request->input('address');

$restaurant->user_id = $request->input('user_id');

$restaurant->verified = $request->input('verified');

$restaurant->status = $request->input('status');

if ($request->hasFile('image')) {

$image = $request->file('image');

$filename = 'restaurant' . '-' . time() . '.' . $image->getClientOriginalExtension();

$location = public_path('images/');

$request->file('image')->move($location, $filename);

$restaurant->image = $filename;

}

// menu

$newArray = array();

$menuArray = $request->custom_menu; //Contains an array of Menu Values

$priceArray = $request->custom_price; //Contains an array of Price Values

//Creating new array with ARRAY KEY : MENU VALUES and ARRAY VALUE: PRICE VALUES

foreach ($menuArray as $key => $singleMenu) {

$newArray[$singleMenu] = $priceArray[$key];

}

//Output : array("Menu01" => "Price01", "Menu02" => "Price 02", "Menu03" => "Price 04", "Menu04" => "Price 05")

//Converting array to json format to store in your table row 'custom_menu_price'

$jsonFormatData = json_encode($newArray);

//Output like: {"Menu01":"Price01","Menu02":"Price 02","Menu03":"Price 04","Menu04":"Price 05"}

// Save in DB

$restaurant->custom_menu_price = $jsonFormatData;

// menu

$restaurant->save();

$restaurant->workdays()->sync($request->workdays, false);

$restaurant->worktimes()->sync($request->worktimes, false);

//Display a successful message upon save

Session::flash('flash_message', 'Restaurant, '. $restaurant->title.' created');

return redirect()->route('restaurants.index');

}

在你的front.restaurants视图内:

@php

// To retrieve back from DB to MENU and PRICE values as ARRAY

$CustomArray = json_decode($restaurant->custom_menu_price, TRUE);

@endphp

@foreach ($CustomArray as $menu => $price)

Menu Name: {{ $menu }}

Menu Price: {{ $price }}

@endforeach

希望它有意义.

标签:php,laravel

来源: https://codeday.me/bug/20190627/1306246.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值