我是laravel的新手,我喜欢有一个包含数据库表中值的droplist。我怎么能这样做,实际上我有一个错误:"未定义的变量:声明" (我认为数据没有通过) - 我认为这是因为几个原因:因为View功能或不同的原因。
如果你有一个下载列表代码,你可以用详细信息解释你的代码 - 请与我分享!
Example of a droplist I like to have(taking the values from the db):
我的值和变量的定义:
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';
这是我的Controller,名称为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
我的路线:
Route::post('/mainpage', 'MainController@index');
实际上,看了很多文章,请解释一下它是如何工作的。
谢谢!
我希望这篇文章也能为不同的编码员提供帮助
来自不同用户的经过测试的方法:
class MainController extends Controller
{
public function index()
{
$claims = Drop::all();
dd($claims);
return View::make('mainpage')->with(claims);
}
在视图中(测试方法):
@foreach($claims as $claim)
<option value="$claim->id">{{$claims->type}}</option>
@endforeach
答案 0 :(得分:2)
对于控制器中的索引方法,你有两个错误
$ claims = Drop :: all() - &gt; get(); //使用此get()
返回View::make('mainpage')->with('users', claims);
//你不需要$
你从哪里得到这个用户
并且在视野中你需要foreach
@foreach($claims as $claim)
<option value="$claim->id">{{$claims->type}}</option>
@endforeach
而且我认为你的模型不会因为命名约定而在db中看到你的表。型号名称是您的db表名称的复数。如果您有表claimTypes模型是claimType
答案 1 :(得分:0)
对不起,代码是正确的。感谢Zoran Kabic的帮助,我解决了这个问题。
我的错误是:对于每个表单路由,我们只能调用一个控制器。例如,我们无法添加少量控制器。
路由:: get(&#39; /&#39;,&#39; MainController @ index&#39;);
路由:: get(&#39; /&#39;,&#39; DropController @ index&#39;); //不正确,每条路径只有一个控制器
我的代码,它已经很好地进行了测试,测试结果为146%:
The result of my code, values are taking from the db
路由:
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):
namespace App;
use Illuminate\Database\Eloquent\Model;
use Eloquent;
class Drop extends Eloquent
{
public $table = 'claim_type'; //table name in your DB
}
控制器(在我的案例中为MainController.php)
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
}
谢谢大家! 希望我的回答将来会有所帮助!