lundi 27 janvier 2020

Problem to create a good SEO friendly route with Laravel

I have a SEO project with Laravel, I want to use the routes to config a friendlys dynamic urls. This is my route:

# Designs
    Route::get('/d/{article}-{tag}-{design_name}-{design_id}',['as' => 'web.product_design', 'uses' => 'ProductController@getProductDesign']);

I want to build this SEO friendly url: /mug-harry-potter-wingardium-leviosa-xfdsfsdf

And that's what I call the route into any laravel blade view:

 route('web.product_design',['article' => 'mug'), 'tag' => str_slug('Harry Potter'), 'design_name' => str_slug('Wingardium Leviosa'), 'design_id' => 'xfdsfsdf'])

The problem is that inside the ProductController I don't receive these parameters as I would like. I think Laravel confuses when it starts and when it finishes the slugs. For example, in the controller method...

# Product Design Page
    public function getProductDesign($article,$tag,$design_name,$design_id) {
      dd($article); // It gives me back 'mug', that's right.
      dd($tag); // It return me 'harry', is WRONG, I want to get 'harry-potter'.
      dd($design_name); // It return me 'potter', is WRONG, I want to get 'wingardium-leviosa'.
      dd($design_id); // It return me 'wingardium-leviosa-xfdsfsdf', is WRONG, I want to get 'xfdsfsdf'.
     }

How can I build a url SEO friendly and at the same time be able to take the parameters correctly within the controller method?



via Chebli Mohamed

How can i open excel file in browser using laravel [closed]

i have page where user can upload attachment in excel file. now he want to see that attachment, when he click on view button at that time attach file open in new tab of browser.



via Chebli Mohamed

try to get response json ,Laravel Pagination has missing meta and links with API resource

Trying

     $questions=TheQuestion::paginate(5);
     return $this->successResponce(TheQuestionResource::collection($questions),'','');

{ success: true,data: [{},{},{},{},{}],tag: "",message: ""}

public function successResponce($data,$tag,$message)
{


      return response()->json([
            'success' => true,
            'data' => $data,
            'tag'=>$tag,
            'message' => $message,
        ]);


}

API Resource is working fine but pagination data like current_page,total... is not showing. But When returning without JSON Response all things are coming

     $questions=TheQuestion::paginate(5);
     return TheQuestionResource::collection($questions);
{
data: [],
links: {
first: "http://localhost:8000/api/v1/admin/question/management/show/%7B%7D?page=1",
last: "http://localhost:8000/api/v1/admin/question/management/show/%7B%7D?page=3",
prev: null,
next: "http://localhost:8000/api/v1/admin/question/management/show/%7B%7D?page=2"
},
meta: {
current_page: 1,
from: 1,
last_page: 3,
path: "http://localhost:8000/api/v1/admin/question/management/show/%7B%7D",
per_page: 5,
to: 5,
total: 11
}
}


via Chebli Mohamed

Laravel - ErrorException : Array to string conversion

guys, I have a problem in this code in Laravel when using php artisan route:list

    ErrorException  : Array to string conversion

  at /var/www/html/app/Http/Controllers/Merchant/CountryAreaController.php:312
    308|             }
    309|         }
    310|         if (!empty($request->rental_service)) {
    311|             $service[] = 2;
  > 312|             foreach ($request->get('rental_vehicle_type' . []) as $item) {
    313|                 $area->VehicleType()->attach($item, ['service_type_id' => 2]);
    314|             }
    315|             $area->Package()->attach($request->rental_service, ['service_type_id' => 2]);
    316|         }


via Chebli Mohamed

Localization is not working for all url in Laravel

Localization is not working for some URL in my Laravel project.

Here are some routes

route A : Route::get('/doctor', 'homeController@doctor');

route B : Route::get('/doctor/{data}', 'homeController@doctor_detail');

localization url :

Route::get('locale/{locale}',function ($locale){

    Session::put('locale',$locale);

    return redirect()->back();

});

Custom Error Route:

 Route::any('{catchall}', function() {

  return App::call('App\Http\Controllers\errorController@error');

})->where('catchall', '.*');

Localization works for route A but gives error for route B.

Can't find any solution yet.

Anybody help please ? Thanks in advance.

If anything need regarding the issue please tell me, I will provide.



via Chebli Mohamed

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