vendredi 6 juillet 2018

Laravel search issue in relationship

I have search functionality in laravel

$product=ProductItem::where(function ($query)use($fromDate,$toDate){
            if($fromDate!= ''&& $toDate!=''){
                $query->whereBetween(DB::raw("DATE(purchased_date)"),[$fromDate,$toDate]);
            }
        })->with(['itemName'=>
            function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='item_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            }
        ,'itemBrand'=> function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='brand_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            },'itemModel'=>function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='item_model_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            },'vendor'=>function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='vendor_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            }])




            ->get();

the above code if i search for itemName then it will return all records with emtpy itemNames for other than search items enter image description here In the screenshot i have searched for itemName nargis but it returned other itemnames with empty itemName and also i tried whereHas nut if foreign key null then it wont return any records

 $product=ProductItem::where(function ($query)use($fromDate,$toDate){
            if($fromDate!= ''&& $toDate!=''){
                $query->whereBetween(DB::raw("DATE(purchased_date)"),[$fromDate,$toDate]);
            }
        })->with(['itemName','itemBrand','itemModel','vendor'])
            ->whereHas('itemName', function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='item_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            })
            ->whereHas('itemModel', function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='item_model_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            })
            ->whereHas('itemBrand', function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='brand_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            })
            ->whereHas('vendor', function($query) use($searchInput,$filterBy) {
                if($filterBy!='all'&&$filterBy=='vendor_name'&&$searchInput!=''){
                    $query->where($filterBy,'LIKE',"%{$searchInput}%");
                }

            })
            ->get();

Can any one help me how i can add search functionality in laravel relationship



via Chebli Mohamed

Laravel Method filter does not exist when updating 5.1 to 5.2

I am getting this error while updating Laravel 5.1 to 5.2.

BadMethodCallException in Macroable.php line 74: Method filter does not exist

any help ?



via Chebli Mohamed

jeudi 5 juillet 2018

laravel proplem with Model::with appended column

i try to add appended column with Model::with

$pitchChech =  Pitch::with(['prices'=>function ($query){
            $query->select('*',\DB::Raw("(select sum(HOUR(TIMEDIFF(valid_to ,valid_from))) from pitch_price as xpitch_price where pitch_id = pitch_price.pitch_id ) as x "))
            ->whereRaw(" x < ?",[24]);
        }])->get();
      dd($pitchChech);

and i have this output

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'x' in 'where clause'  

i can't understand what is the problem im using laravel 5.1 but i don't think there are problem with ut



via Chebli Mohamed

Laravel routes not working with subfolder

Using Laravel 5 migrated from 4.2 now laravel 5 is installed in subfolder "abc" do i have to write abc/warehouse for every route ? previously it was /warehouse. i want to use all existing routes like /warehouse inside subdirectory abc.

i am on localhost xampp with port 81. http://localhost:81/warehouse

any one here with quick solution



via Chebli Mohamed

mardi 3 juillet 2018

Laravel: display multi data in one row for every user

How I can display multi data in one row with foreign key by id user for example: I have users with multi ticket, i need to display for every user number of his tickets in one column.

enter image description here

this my controller

public function followticket()
{
    $data = DB::table('tickets')
        ->join('users', 'users.id', '=', 'tickets.user_id')
        ->select('users.*', 'tickets.*')->get();

    return view('Admin.suivi-tickest',compact('data'));
}



via Chebli Mohamed

Issue in retrieving the data after submission of the form in the same page in laravel

Kindly help me with this issue. Before submitting the form in a variable called "$data" I have the data as below:

{ "message": "Required parameter missing: domain-name", "status": "ERROR" }

And after submitting the value in the form, in the same variable called "$data", I have the below data.

 {"decksys.com":{"classkey":"domcno","status":"available"}}

Now I need to display the "decksys.com" and the status "available".Find below my code and kindly help me to solve this issue.

Controller Code:

  class AvailableController extends Controller
  {

     public function create(Request $request)
     {

        $domainname = Input::get('domainname');
        $tld = Input::get('tld');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, 'https://httpapi.com/api/domains/available.json?auth-userid=711757&api-key=74miSZPgYkkc108Ptmeo5Q8iDTtKk3xi&domain-name='.$domainname.'&tlds='.$tld.'');
        $result = curl_exec($ch);
        curl_close($ch);
        $data = json_encode($result);
        $response = json_decode($data);
       // $result = json_decode($response);
       $data = json_decode($response, true);

        return view('clientlayout.main.tld',compact('data'));

     }
    }

View Page:

     @foreach($data as $key => $value)

     @if ($data['status'] == 'ERROR')

       

     @endif


     @endforeach

Here it checks the status correctly and displays "ERROR". But after submitting the form I'm getting the error as: "Undefined index: status ". Kindly help me to solve this issue.



via Chebli Mohamed

lundi 2 juillet 2018

Laravel data filtering and sorting with pagination

I am developing a laravel project which has a function to search tournaments. it has a view to list all tournaments. then it has functionalities to search by id, name, category etc. i want to sort this list A-Z with filtered results also. how can I do that?table has over 1 million records.when i user orderBy('table.name','asc'), it takes a lot time. this query uses paginate as well.what would be the best practice to do this?



via Chebli Mohamed