Dmitry Malys
Dmitry Malys

Reputation: 1323

Laravel "if else" check if exist in other table

I want to use this if else statement like this:

    @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

This is my controller:

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'
        ));

    }

My database structure is:

comments table: id, lot_id, 
ourcars table: id, lot_id

My models: Comment:

public function OurCar()
    {
        return $this->hasMany(OurCar::class);
    }

OurCars:

public function Comment()
    {
        return $this->belongsTo(Comment::class);

    }

OurCars migration:

Schema::create('ourcars', function (Blueprint $table) {
            $table->engine = 'MyISAM';
            $table->increments('id');
            $table->integer('lot_id')->unsigned();

and it same for comments

What im trying to do is check if the lot_id already exist in "ourcars" table. If exist than return that message that this car is already saved. If not, than echo form.

With my code i have this error:

SQLSTATE[HY000]: General error: 2031 (SQL: select * from ourcars where lot_id = ? limit 1)

Can some one recommend me a better solution?

Upvotes: 0

Views: 959

Answers (1)

SpyrosJevan
SpyrosJevan

Reputation: 80

The reason you are getting this message is because the get method will return an array , in this case it will bring all the lines of table commentplus it need 1 more argument at least to function.

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

Also change your models to this

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

And this

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

    }

Here an example on how can you do it based on your code.

Pass the lot_id on the request

 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);

     }

Here the view

    @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

Upvotes: 1

Related Questions