lundi 27 janvier 2020

Correct way to set up this Laravel relationship?

I'm after a bit of logic advice. I am creating a system where users login and register their participation at an activity. They can participate at an activity many times. What is the best way to do this? I want to ensure I can use eloquent with this rather than creating my own functions.

I am imagining...

Users:

id

Activitys:

id
name

Participations:

id
user_id
activity_id
time_at_activity

I want to later be able to do such things as: $user->participations->where('activity_id', 3) for example.

What is the best way to set this up? I had in mind..

User: hasMany->Participations

Activity: belongsTo->Participation

Participation: hasMany->Activitys & belongsTo->User

Does this look correct?



via Chebli Mohamed

Xero oAuth 2 authorisation in laravel

I have a Laravel PHP project application. The server creates an invoice on Xero and sends the user an email etc...

I have been using OAuth 1 (Private App - PHP SDK) with no issues, but now need to switch to OAuth 2. As there is no front end-user on the server, can this still be accomplished?

All the documentation I read, seems to need a manual login to grant authorization and get an access token?



via Chebli Mohamed

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: /d/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

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

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 an error for route B.

Can't find any solution yet.

Anybody help, please? Thanks in advance.

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



via Chebli Mohamed