doulai7239 2014-04-14 00:45
浏览 60
已采纳

在Laravel 4中进行查询呼叫的最佳位置在哪里?

First of all, Everything is kind of confusing. There is a few tutorials and complete documentation but i feel like something is just not working way it should. For now I'm a little bit confused and discouraged. I learned how to write forms, how to make views with blade. How to menage migrations and even seeds. I know how to create new controller and simple routes.

But guys... I need some advice if you would answer...

I created a form with a view for example that form:

{{Form::open(array('url' => 'person/confirm'))}}

{{Form::label('firstname', 'Firstname:')}}
{{Form::text('firstname')}}

{{Form::label('lastname', 'Lastname:')}}
{{Form::text('lastname')}}

{{Form::label('company_name', 'Company name:')}}
{{Form::text('company_name')}}

{{Form::label('mail', 'Mail:')}}
{{Form::text('mail')}}

{{Form::label('phone', 'Phone:')}}
{{Form::text('phone')}}

{{Form::label('additional_phone', 'Additional phone:')}}
{{Form::text('additional_phone')}}

{{Form::label('city', 'City:')}}
{{Form::text('city')}}

{{Form::label('postalcode', 'Postalcode:')}}
{{Form::text('postalcode')}}

{{Form::label('address', 'Address:')}}
{{Form::text('address')}}


{{Form::label('notes', 'Notes:')}}
{{Form::text('notes')}}


{{Form::submit('Submit')}}

{{Form::close()}}

Nothing fancy, just a basic form. If I call submit, it will take me to person/confirm route. And it will be like this:

Route::post('person/confirm', function(){

   $input = Input::all();

    DB::table('humanity')->insert(

       array('firstname' => $firstname);

    );

}

I know that it is wrong.

How to get values from $input?

How to insert it to table correctly and safely?

Where is the best place to make query call?

It will be better to make query before routing starts or when next route will execute?

It would be good to create query in controller function and execute it in route then redirect to "message: Success"?

What exactly are classes and models and how can I use them?

I plan to write some system and day by day it makes me more sicker than satisfied. Something call me that I had hitched my wagon to a star.

Thank you in advice.

  • 写回答

2条回答 默认 最新

  • drvpvmz16241016 2014-04-14 01:20
    关注

    Ok so first off I suggest you look into Eloquent. Laravels ORM (http://laravel.com/docs/eloquent).

    Eloquent will allow you to do this:

    $human = new Human(Input::all());
    $human->save();
    

    But we can come back to that, your first question here is how to use the Input class. The real documentation can be found under Requests in the laravel documentation, but here's a brief guide.

    Input::get('firstname');            //gets the first name
    Input::get('lastname');             //get doesn't refer to the method, "get" will retrieve from post or get.
    Input::all();                       //will get you all input as an array - don't forget to validate
    Input::except(array('csrf_token')); //will get you everything except for the 'csrf_token' as an array 
    

    One of the best ways to get help with Laravel is through the Laravel IRC channel, the community is great.

    #Laravel is a place where many developers hang out and discuss the framework. Many people frequently pop their heads in looking for some help.

    But, the real-time chat nature of IRC can be a refreshing contrast to posting on a forum and waiting for a reply.


    The official community hub for laravel

    Laravel.io has forums along with a plethora or useful tools such as the pastebin, you should become friends.


    The other resource I suggest is Taylor's book

    It's not free but it's well worth it.

    Written by the creator of Laravel, this is the definitive guide to advanced application development with Laravel 4. Learn about dependency injection, interfaces, service providers, SOLID design, and more while exploring practical, real-world code examples. Whether you're building a robust, large application with the Laravel framework, or just want to sharpen your software design chops, this book will be of great value to you and your team.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法