0

I'm trying to make search function and having problem. I have Array to string conversion error message. Could you teach me right code please?

Here is this program usage

1.User select value then click [search] button (This is search.blade.php)

2.Search result will display at result.blade.php

my Laravel Framework is 6.18.8

search.blade.php

    <form action="{{ route('search') }}" class="form-image-upload" method="POST" enctype="multipart/form-data">
 {!! csrf_field() !!}

    <div class="col-md-5">    
                <strong>TYPE</strong>    
                <select name="type" class="form-control">
                <option value="-" selected>-</option>
                <option value="j">j</option>
                <option value="w">w</option>
                </select>         
            </div>

            <div class="col-md-5">
                <strong>wc</strong>
                <select name="wc" class="form-control">
                <option value="N0" selected>0</option>
                <option value="N1">1</option>
                <option value="N2">2</option>
                <option value="N3">3</option>    
                </select> 
            </div>
             <div class="col-md-5">
                <strong>FC</strong>
                <select name="fc" class="form-control">
                <option value="0" selected>0</option>                   
                <option value="f01">f01</option>
                <option value="f02">f02</option>
                <option value="f03">f03</option>    
                </select>      
            </div>
            <div class="col-md-5">
                <strong>YC</strong>
                <select name="yc" class="form-control">
                <option value="0" selected>0</option>                   
                <option value="yc1">yc1</option>
                <option value="yc2">yc2</option>    
                </select>     
            </div>
            <div class="col-md-5">
                <strong>SC</strong>
                <select name="sc" class="form-control">
                <option value="Z01" selected>Z01</option>                   
                <option value="Z02" selected>Z02</option>
                <option value="Z03" selected>Z03</option>
                </select>      
            </div>
      <div class="col-md-2">

            <br/>
            <button type="submit" class="btn btn-success">Search</button>    
        </div>    
    </div>   
</form> 

result.blade.php

 {!! csrf_field() !!}
<div class='list-group gallery'>
        @if($images->count())
            @foreach($images as $image)
            <div class='col-sm-4 col-xs-6 col-md-3 col-lg-3'>
            <a class="thumbnail fancybox" rel="ligthbox" href="/images/{{ $image->image }}">
                <img class="img-responsive" alt="" src="/images/{{ $image->image }}" />
                <div class='text-center'>
                    <small class='text-muted'></small>
                </div> <!-- text-center / end -->
            </a> 
        </div> <!-- col-6 / end -->
        @endforeach
    @endif
</div> <!-- list-group / end -->

ImageGalleryController.php

public function search()

{
    $images = ImageGallery::get();
    return view('search',compact('images'));
}

public function order(Request $request)
{

    $data = $request->all();
    $images = ImageGallery::where(['type',$request->$data['type']],
        ['wc',$request->$data['type']],
        ['fc',$request->$data['fc']],
        ['yc',$request->$data['yc']],
        ['sc',$request->$data['sc']])->get();

    return view('result',compact('images'));
}  

Web.php

// search section
Route::post('search', 'ImageGalleryController@order');
Route::get ('search', 'ImageGalleryController@search');

UPDATE

Curent my controller

public function search()

    {
        $images = ImageGallery::get();
        return view('search',compact('images'));
    }

    public function order(Request $request) {

        $data = $request->all();

        $images = ImageGallery::where([
            ['type', $data['type']],
            ['wc',$data['type']],
            ['fc',$data['fc']],
            ['yc',$data['yc']],
            ['sc',$data['sc']]
        ])->get();

         dd($images); 
        return view('search', compact('images'));

        }

3 Answers 3

1

Check this code:

search.blade.php

<form action="{{ route('search') }}" class="form-image-upload" method="POST" enctype="multipart/form-data">
    {!! csrf_field() !!}

    <div class="col-md-5">    
        <strong>TYPE</strong>    
        <select name="type" class="form-control">
            <option value="" selected>Please Select</option>
            <option value="j">j</option>
            <option value="w">w</option>
        </select>         
    </div>

    <div class="col-md-5">
        <strong>wc</strong>
        <select name="wc" class="form-control">
            <option value="" selected>Please Select</option> 
            <option value="N0">0</option>
            <option value="N1">1</option>
            <option value="N2">2</option>
            <option value="N3">3</option>    
        </select> 
    </div>
    <div class="col-md-5">
        <strong>FC</strong>
        <select name="fc" class="form-control">
            <option value="" selected>Please Select</option>                   
            <option value="f01">f01</option>
            <option value="f02">f02</option>
            <option value="f03">f03</option>    
        </select>      
    </div>
    <div class="col-md-5">
        <strong>YC</strong>
        <select name="yc" class="form-control">
            <option value="" selected>Please Select</option>                   
            <option value="yc1">yc1</option>
            <option value="yc2">yc2</option>    
        </select>     
    </div>
    <div class="col-md-5">
        <strong>SC</strong>
        <select name="sc" class="form-control">
            <option value="" selected>Please Select</option>
            <option value="Z01">Z01</option>                   
            <option value="Z02">Z02</option>
            <option value="Z03">Z03</option>
        </select>      
    </div>
    <div class="col-md-2">

        <br/>
        <button type="submit" class="btn btn-success">Search</button>    
    </div>   
</form>

result.blade.php

<div class='list-group gallery'>
    @if($images->count())
        @foreach($images as $image)
        <div class='col-sm-4 col-xs-6 col-md-3 col-lg-3'>
            <a class="thumbnail fancybox" rel="ligthbox" href="/images/{{ $image->image }}">
                <img class="img-responsive" alt="" src="/images/{{ $image->image }}" />
                <div class='text-center'>
                    <small class='text-muted'></small>
                </div> <!-- text-center / end -->
            </a> 
        </div> <!-- col-6 / end -->
        @endforeach
    @endif
</div> <!-- list-group / end -->

ImageGalleryController.php

public function search()
    {
        $images = \App\ImageGallery::get();

        return view('search', compact('images'));
    }

    public function order(Request $request)
    {

        $data = $request->all();

        $images = \App\ImageGallery::when($data['type'], function ($query, $type) {
            return $query->where('type', $type);
        })->
        when($data['fc'], function ($query, $fc) {
            return $query->orWhere('fc', $fc);
        })->
        when($data['yc'], function ($query, $yc) {
            return $query->orWhere('yc', $yc);
        })->
        when($data['wc'], function ($query, $wc) {
            return $query->orWhere('wc', $wc);
        })->
        when($data['sc'], function ($query, $sc) {
            return $query->orWhere('sc', $sc);
        })
        ->get();

        return view('result', compact('images'));
    }

This is the database structure:

enter image description here

This is the full working code on my local system.

This is web.php file

Route::get ('search', 'ImageGalleryController@search');
Route::post('search', 'ImageGalleryController@order')->name('search');
Sign up to request clarification or add additional context in comments.

6 Comments

@greeniron check this solution.
Dear @Ankur Mishra Thank you very much! I'm checking right now ... now I got routing error . Could you show me web.php POST and GET code ?
@greeniron check the updated answer. It now contains your web.php file content.
Dear @Ankur Mishra Thank you very much!!! This is perfect! Amazing!!! I'm so happy! Thank you aganin! . May I ask one thing? It's my bad. I told wrong sql. Sorry. I would like to make sql like this "select * from image_gallery where (type = ? or wc = ? or fc = ? or yc = ? or sc = ?)" . Could you teach me please ? This is last favor.
@greeniron pls check the updated answer. Let me know if you face any issues.
|
1
  1. Add csrf token in your form.
@csrf
  1. Change order function as:

    public function order(Request $request) {

    $data = $request->all();
    
    $images = ImageGallery::where([
        ['type', $data['type']],
        ['wc',$data['type']],
        ['fc',$data['fc']],
        ['yc',$data['yc']],
        ['sc',$data['sc']]
    ])->get();
    return view('result', compact('images'));
    

    }

  2. Give name method to route:

Route::post('search', 'ImageGalleryController@order')->name('search');

13 Comments

@greeniron pls check the answer and let me know it work or not.
Dear @Ankur Mishra Thank you so much for answer and I'm sorry to late reply. Array to string conversion Error message is gone now. but I have another problem.I change my code as you taught me. and now I got this error "Facade\Ignition\Exceptions\ViewException Call to a member function count() on string " so I tryed to check what is happing, I add ->toSql(); var_dump($images); Then I got string(100) "select * from image_gallery where (type = ? and wc = ? and fc = ? and yc = ? and sc = ?)" I selected all value but why I got this query?
Change $images->count() to $images->first() in blade.
Dear @Ankur Mishra I changed. now I got this error Facade\Ignition\Exceptions\ViewException Call to a member function first() on string. And query also same string(100) "select * from image_gallery where (type = ? and wc = ? and fc = ? and yc = ? and sc = ?)"
You are not getting eloquent collection as a result. You are getting results as a string. Can you check what you get $images? Please use this in your controller: dd($images);
|
0

You have to change something. Because your $data is an array. But you have declare as an object. For this reason you get this error.

$data = $request->all();

$images = ImageGallery::where(['type'=>$data['type']],
    ['wc'=>$data['type']],
    ['fc'=>$data['fc']],
    ['yc'=>$data['yc']],
    ['sc'=>$data['sc']])->get();

1 Comment

Dear @A.A Noman Thank you very much for helping me. I replace my code but I got same error...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.