【问题标题】:Laravel "if else" check if exist in other tableLaravel“if else”检查是否存在于其他表中
【发布时间】: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 ourcars lot_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


【解决方案1】:

您收到此消息的原因是因为 get 方法将返回一个数组,在这种情况下,它将带来表 comment 的所有行加上它至少需要 1 个参数才能运行。

$lot_id = Comment::where('lot_id')->get(); //

也将您的模型更改为此

public function OurCar()
    {
        return $this->hasMany('App\Comment');
    }

还有这个

public function Comment()
    {
        return $this->belongsTo('App\OurCar');

    }

这里有一个示例,说明如何根据您的代码进行操作。

request 上传递lot_id

 public function browse(Request $request, CommentFilters $filters)
        {
            $ourcar     = OurCar::where('lot_id',$request->lot_id)->first();
            $comments   = Comment::filter($filters)->orderBy('lot_date', 'desc')->paginate(30)->appends($request->all());

            return view('comments.browse')->with('ourcar',$ourcar)->with('comments',$comments);

     }

这里是风景

    @foreach ($comments as $comment)
<tr>
<td>    
    @if ($ourcar->lot_id != $comment->lot_id)
           <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

【讨论】:

  • 也许我所有的概念都是错误的?正如我之前所说,我尝试在我的函数中实现这个概念:link
  • 要回答这个问题,我需要更多关于您要在浏览功能上显示什么的信息。会有一个评论部分,人们可以选择汽车来标记 cmets 什么的?整个概念对我来说有点模糊。
  • 我试图将信息表格“评论”添加到表格“OurCars”,但在添加之前我想检查此信息(汽车)是否已经存在于表格“OurCars”中
  • 不幸的是,我的模型有问题:调用模型 [App\OurCar] 上的未定义关系 [lot_id]。
  • 你能在你的问题上添加模型吗?
猜你喜欢
  • 1970-01-01
  • 2019-01-18
  • 2018-03-04
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2013-07-21
相关资源
最近更新 更多