laravel how to specifiy the input columns

余生长醉 提交于 2019-12-14 03:12:06

问题


I have a form in html that has an input with name restaurantName, but my column in the database is name. I am not able to use this in the controller

Restaurant::create(Input::only('restaurantName'));

Because there is no column with restaurantName so my question is how to tell laravel that restaurantName is maped to name ?

many thanks

Edit

the html form has these fields:

restaurantName
website
username
password
mobileNumber
firstName
lastName

The database has two tables which are Admin and Restaurant, each restaurant has one admin and the admin is one for each restaurant, so it is one to one relationship

when I submit the form I do the following:

$input = Input::all();
 $admin = Admin::create(Input::only('username', 'password', 'mobileNumber', 'firstName', 'lastName'));

                $data = ['name' , Input::get('restaurantName'), 'website' => Input::get('website')];
                $restaurant = new Restaurant($data);
                $admin->restaurant()->save($restaurant);

the admin row is inserted to the database so the problem starts with the $data line code.

the exception is:

BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::save()

回答1:


Firstly, you should never just blindly pass user input to models, and secondly, this particular problem has absolutely nothing to do with laravel.

Your problem can be solved with basic PHP. Simply, create your own array of input.

$data = [
    'field'    => Input::get('field'),
    'name'     => Input::get('restaurantName')
];

There seems to be a grave misconception that Laravel is its own independant system, it is not. It is literally a collection of PHP code, it doesn't do anything that can't be done with PHP and it doesn't prevent you from doing anything you can normally do with PHP.



来源:https://stackoverflow.com/questions/24283510/laravel-how-to-specifiy-the-input-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!