dimanche 26 janvier 2020

Why isn't my condition working in search?

I am trying to get a searched result and I have the following code:

public function search(Request $request){ 
    $from = $request->from;
    $to = $request->to;
    $word = $request->word;  
    if(empty($word) || $word == null){
        $searched = Post::whereBetween('created_at', [$from, $to])->paginate(10);
    } elseif(!empty($word) && !empty($from) && !empty($to)){ 
        $searched = Post::where('title', 'LIKE', '%' . $word . '%')
        ->orWhere('content', 'LIKE', '%' . $word . '%')
        ->orWhere('subtitle', 'LIKE', '%' . $word . '%')
        ->whereBetween('created_at', [$from, $to])
        ->paginate(10); 
    } elseif(empty($from) && empty($to) && !empty($word)){
        $searched = Post::where('title', 'LIKE', '%' . $word . '%')
        ->orWhere('content', 'LIKE', '%' . $word . '%')
        ->orWhere('subtitle', 'LIKE', '%' . $word . '%')
        ->paginate(10);
    }

    return view('page.search', compact('searched', 'from', 'to'));
}

The first and second condition are working but not the last. What am I missing or doing wrong here?



via Chebli Mohamed

MethodNotAllowedHttpException:The GET method is not supported for this route. Supported methods: POST

I am new to laravel and now i am facing an issue. I saw many related answers ,but nothing works for me. My problem is, i have a page and when i update a user i want to redirect on the same page with updated results.When i look into the db table,the updation is happening,but the page shows the above error. I tried many answers which i saw on stack , but nothing works for me.

Thank you

Here is my view:

<form method="POST"  action="/updateleaduser">

    @csrf

    <h6 style = "font-family:Palatino" class="card-text">Assigned To:&nbsp; </h6>
    <input type="hidden" name="idd" name="idd" value="">
    <select name="select_user" class="form-control">
        @foreach($testusers as $user) 
        <option value=""></option>
        @endforeach
    </select>
    <button type="submit" class="form-control" style="background-color: green;color: white;">Update User</button>

Here is my route:

Route::post('/updateleaduser','RequestController@updateuserlead')->name('updateleaduser');

Here is my controller:

public function updateuserlead(Request $request){
    $idd = $_POST['idd'];
    $select_user = $request->input('select_user');

    DB::table('leads')->where('id',$idd)->update(array(
        'client_id'=>$select_user,
    ));
    return redirect()->back();
}


via Chebli Mohamed

Undefined offset: 2 when I delete an element of the array and I want to return the rest of them

I am getting this error whenever I try to delete and element in the array. It only work when I delete everything. But I want to delete one by one. this is delete function

 public function delete($id){

      $user_id = auth() ->user()->id;
      $card = travel_plan::all()->where('travel_id', $user_id);
      foreach($card as $cardId){
        $cardd = $cardId;
    }  //->where('city_name', $uo)->first();
      $cardd->delete();

  return redirect('/home')->with('success', 'Post Removed');

}

this is my delete button

 {!!Form::open(['action'=>['TravelPlanController@delete','id' =>$cardd[]],'method'=>'POST','class'=>''])!!}

this is how I retrieved the id

 $cardd = array();
      $card = travel_plan::all()->where('travel_id', $user_id);
     foreach($card as $cardId){
          $cardd [] = $cardId ->id;
      }


via Chebli Mohamed

Order by CASE with different column values in Laravel does not work

I am trying to build a laravel query which should order values by more columns: first of all, by hour and this works and also by values in a column named "competition_type" in this way: 'ATP - SINGLES', 'WTA - SINGLES', 'CHALLENGER MEN - SINGLES', 'CHALLENGER WOMEN - SINGLES', 'ITF MEN - SINGLES', 'ITF WOMEN - SINGLES' and I tried this query, but it does work...I tried several approaches and this is my last one:

$matches = Match::select()
           ->where('date', $date)->where('pick_score', '<>', '0');

    $matches = Match::where('date', $date)
                ->where('pick_score', '<>', '0');

    $matches = Match::select("CASE 
                                WHEN competition_type like '%ATP - SINGLES%' then 1
                                WHEN competition_type like '%WTA - SINGLES%' then 2 
                                WHEN competition_type like '%CHALLENGER MEN - SINGLES%' then 3
                                WHEN competition_type like '%CHALLENGER WOMEN - SINGLES%' then 4
                                WHEN competition_type  '%ITF MEN - SINGLES%' then 5
                                WHEN competition_type = '%ITF WOMEN - SINGLES%' then 6 
                                       END DESC")
                  ->select('matches.*');

The thing is that if I add an "order by" before the case, it gives me an error. How should I change my query in order to have the column values in the way I mentioned?



via Chebli Mohamed

How to Make RealTime sulution for ReactJs + Laravel?

I want to make real-time connection between React.JS (frontend) and Laravel as (Api server provider) any Advice for this to do that ?? pusher or Socket.io ? i want to make comment Post system with realtime and realtime chat



via Chebli Mohamed

Laravel package error on file uploading using Spatie\MediaLibrary

i just try to upload the image to the server using Spatie\MediaLibrary package .Then laravel gives this error of not found function. I tried all the solution no one worked

This is the code that i am using

> public function update(Request $request, Channel $channel)
>     {
>        if($request->hasFile("image")){
>          $channel->addAllMediaFromRequest('image')->toMediaCollection('images');
>        }
>     }

please click here to see the error message

use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use \YouTube\Model;
use App\User;

class Channel extends Model implements HasMedia
{
    use HasMediaTrait;
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

this is my model



via Chebli Mohamed

Cannot get data from collection in Laravel

I want to get data from collection i am getting following two arrays in collection when i run this code:


enter image description here

and i want to get data through loop for that i am using following code:

Code:

 @foreach ($child_categories as $index => $element)
  
@endforeach


via Chebli Mohamed