数据库中的Laravel droplist

数据库中的Laravel droplist,laravel,laravel-5,Laravel,Laravel 5,我是laravel的新手,我喜欢用数据库表中的值创建一个droplist。我该怎么做,实际上我有一个错误:“未定义变量:声明”(我认为数据没有传递)-我认为这是因为几个原因:因为视图函数或不同的原因 如果你有一个droplist代码,你可以详细解释你的代码-请与我分享 我的值和变量的定义: mainpage - my html page Drop - my table with the name "claim_type" in the db $claims - variable with

我是laravel的新手,我喜欢用数据库表中的值创建一个droplist。我该怎么做,实际上我有一个错误:“未定义变量:声明”(我认为数据没有传递)-我认为这是因为几个原因:因为视图函数或不同的原因

如果你有一个droplist代码,你可以详细解释你的代码-请与我分享

我的值和变量的定义:

mainpage - my html page

Drop - my table with the name "claim_type" in the db

$claims - variable with info from the table "claim_type" from the database
是的,我用函数声明了我的表

protected $table = 'claim_type';
这是我的控制器,名为MainController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Main;
use App\Drop;

class MainController extends Controller
{
    public function index()
    {
        $claims = Drop::all();
        return View::make('mainpage')->with('users', $claims);
    }
和我的html:

@extends('layout.mainlayout')

@section('content')
<form method="POST" action="{{ url('/') }}/querty">
{{ csrf_field()}}
<div>
         <h2>Force form:</h2>
    <p> You can add you information here, Sir:</p>
    <p> Name:
        <input type="text" name="name" /> 
    </p>
    <p> Email:
        <input type="text" name="email" />
    </p>
    <div>
    <form method="POST" action="{{ url('/') }}/add">
    @foreach($claims as $claim) 
    <option value="$claim->id">{{$claim->type}}</option> 
    @endforeach
    </form>
    </div>
    <p> Where:
        <input type="text" name="where" />
    </p>
    <p> Description:
        <input type="text" name="description" />
    </p>
    <button type="submit" class="btn btn-default">Explose it!</button>

</div>
</form>
@endsection
实际上,我看过很多文章,请给我解释一下它是如何工作的

谢谢大家!

我希望这篇文章也能对不同的程序员有所帮助

来自不同用户的测试方法:

class MainController extends Controller
{
    public function index()
    {
        $claims = Drop::all();
        dd($claims);
        return View::make('mainpage')->with(claims);
    }
在视图中(测试方法):

@foreach($claims as$claims)
{{$claims->type}
@endforeach

控制器中的索引方法有两个错误

$claims=Drop::all()->get();//使用这个get()

return
View::make('mainpage')->带有('users',claims)//您不需要$

你从哪里得到的

鉴于你需要每个人

 @foreach($claims as $claim)
    <option value="$claim->id">{{$claims->type}}</option>
@endforeach
@foreach($claims as$claims)
{{$claims->type}
@endforeach

而且我认为由于命名约定,您的模型在db中看不到您的表。型号名称是数据库表名称的复数形式。如果您有表claimTypes model is claimType

对不起,代码是正确的。多亏了Zoran Kabic我修好了程序错误。p> 我的错误是:对于每个表单路由,我们只能调用一个控制器。例如,我们不能添加几个控制器。

路由::获取('/','MainController@index');

路由::获取('/','DropController@index');//不正确,每个路径只有一个控制器

我的代码运行良好,测试速度为146%:

路由:

Route::get('/', 'MainController@index'); //you need to use get method,
     //and your controller and index (in my case it's MainContoller and @index - index)
模型(在我的例子中,它被命名为Drop.php):

控制器(在我的例子中是MainController.php)

谢谢大家!
希望我的回答将来能对某人有所帮助

亲爱的佐兰·卡比奇,你的方法行不通。是的,laravel看到了我的db,我这样声明:protected$table='claim_type';转储$索赔并向我们显示结果,我确实有关于$的错误。我通常使用compact('users','claims')对不起,如何和在哪里转储$claims?dd($claims);在控制器中,在$claims=Drop::all()之后;若您得到数据,那个么问题就在您的视图中,因为您不使用foreach循环,并且不需要->get();刚开始我在电话里读到了,但看不清楚。请不要在你的问题中包含答案。来自其他用户的答案应该是独立的(并被投票/接受),你自己的任何解决方案也应该包含在答案中。
 @foreach($claims as $claim)
    <option value="$claim->id">{{$claims->type}}</option>
@endforeach
Route::get('/', 'MainController@index'); //you need to use get method,
     //and your controller and index (in my case it's MainContoller and @index - index)
namespace App;

use Illuminate\Database\Eloquent\Model;
use Eloquent;
class Drop extends Eloquent
{
    public $table = 'claim_type'; //table name in your DB

}
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Drop; //using our Model

class MainController extends Controller
{
    public function index()
    {

    $claims = Drop::all();//taking the values from db to variable

    return view('mainpage', ['claims'=> $claims]); //value to the view from the value $claims

    }