【发布时间】:2017-10-28 17:59:30
【问题描述】:
我想像这样使用if else 语句:
@foreach ($comments as $comment)
<tr>
<td>
@if (is_null($ourcar))
<form method="POST" action="/comments/{{ $comment->id }}/ourcars">
{{ csrf_field() }}
<button type="submit" class="btn btn-xs btn-primary">Our cars</button>
</form>
@else
<p>saved</p>
@endif
</td>
</tr>
@endforeach
这是我的控制器:
public function browse(Request $request, CommentFilters $filters)
{
$lot_id = Comment::where('lot_id')->get();
$ourcar = OurCar::where('lot_id', '=', $lot_id)->first();
$comments = Comment::filter($filters)->orderBy('lot_date', 'desc')->paginate(30)->appends($request->all());
return view('comments.browse', compact(
'comments',
'ourcar'
));
}
我的数据库结构是:
comments table: id, lot_id,
ourcars table: id, lot_id
我的模型: 评论:
public function OurCar()
{
return $this->hasMany(OurCar::class);
}
我们的汽车:
public function Comment()
{
return $this->belongsTo(Comment::class);
}
OurCars 迁移:
Schema::create('ourcars', function (Blueprint $table) {
$table->engine = 'MyISAM';
$table->increments('id');
$table->integer('lot_id')->unsigned();
cmets 也一样
我想要做的是检查 lot_id 是否已经存在于“ourcars”表中。如果存在,则返回该汽车已保存的消息。如果没有,则比回声形式。
我的代码出现了这个错误:
SQLSTATE[HY000]: 一般错误: 2031 (SQL: select * from
ourcarslot_id= 哪里?限制 1)
有人可以推荐我一个更好的解决方案吗?
【问题讨论】:
-
返回使用 //
-
@RïshïKêsh Kümar - 我需要返回一个数组,如何使用 WITH?
-
返回视图('cmets.browse',compact('cmets',$cmets),compact('ourcar',$ourcar) )
-
@RïshïKêshKümar。我使用了这个主题,但我想念一些东西 link 我在 laravel 中是新的
标签: php laravel-5.4