Home > Mobile >  Converting Laravel "return view($this->theme" to return Json data
Converting Laravel "return view($this->theme" to return Json data

Time:12-22

I bought this Codecanyon WebApp project which i want to use the APIs to develop a mobile version (fluter & Dart) of same system, only for me to realise theres no API i can use to integrate those features using Flutter and dart for Mobile app... some codes below

api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

web.php ( since the file is long i just copied some part of it

/*====Manage Users ====*/
        Route::get('/users', 'Admin\UsersController@index')->name('users');
        Route::get('/users/search', 'Admin\UsersController@search')->name('users.search');
        Route::post('/users-active', 'Admin\UsersController@activeMultiple')->name('user-multiple-active');
        Route::post('/users-inactive', 'Admin\UsersController@inactiveMultiple')->name('user-multiple-inactive');
        Route::get('/user/edit/{id}', 'Admin\UsersController@userEdit')->name('user-edit');
        Route::post('/user/update/{id}', 'Admin\UsersController@userUpdate')->name('user-update');
        Route::post('/user/password/{id}', 'Admin\UsersController@passwordUpdate')->name('userPasswordUpdate');
        Route::post('/user/balance-update/{id}', 'Admin\UsersController@userBalanceUpdate')->name('user-balance-update');

        Route::get('/user/send-email/{id}', 'Admin\UsersController@sendEmail')->name('send-email');
        Route::post('/user/send-email/{id}', 'Admin\UsersController@sendMailUser')->name('user.email-send');
        Route::get('/user/transaction/{id}', 'Admin\UsersController@transaction')->name('user.transaction');
        Route::get('/user/fundLog/{id}', 'Admin\UsersController@funds')->name('user.fundLog');
        Route::get('/user/payoutLog/{id}', 'Admin\UsersController@payoutLog')->name('user.withdrawal');
        Route::get('/user/escrowLog/{id}', 'Admin\UsersController@escrowLog')->name('user.escrow');


        Route::get('/email-send', 'Admin\UsersController@emailToUsers')->name('email-send');
        Route::post('/email-send', 'Admin\UsersController@sendEmailToUsers')->name('email-send.store');

homeController.php (just copied some methods from it)

 public function addFund()
    {

        $data['totalPayment'] = null;
        $data['gateways'] = Gateway::where('status', 1)->orderBy('sort_by', 'ASC')->get();
        return view($this->theme . 'user.addFund', $data);
    }


    public function profile()
    {
        $user = $this->user;
        $languages = Language::all();
        return view($this->theme . 'user.profile.myprofile', compact('user', 'languages'));
    }

    public function updateProfile(Request $request)
    {

        $allowedExtensions = array('jpg', 'png', 'jpeg');

        $image = $request->image;
        $this->validate($request, [
            'image' => [
                'required',
                'max:4096',
                function ($fail) use ($image, $allowedExtensions) {
                    $ext = strtolower($image->extension());
                    if (!in_array($ext, $allowedExtensions)) {
                        return $fail("Only png, jpg, jpeg images are allowed");
                    }else{
                        if (($image->getSize() / 1000000) > 2) {
                            return $fail("Images MAX  2MB ALLOW!");
                        }
                    }

                }
            ]
        ]);
        $user = $this->user;
        if ($request->hasFile('image')) {
            $path = config('location.user.path');
            try {
                $user->image = $this->uploadImage($image, $path);
            } catch (\Exception $exp) {
                return back()->with('error', 'Could not upload your ' . $image)->withInput();
            }
        }
        $user->save();
        return back()->with('success', 'Updated Successfully.');
    }

How can i get/convert this to return Json that i can use on my flutter project and how easy can it be to implement as a junior dev?

thanks... i just need help on this

CodePudding user response:

ChatGPT answer

It looks like you have some code from a Laravel web application and you want to use the APIs to develop a mobile app using Flutter and Dart. To do this, you will need to set up an API endpoint in your Laravel application that can be accessed from your Flutter app.

Here

  1. Identify the features that you want to implement in your mobile app, such as user management, adding funds, and updating profiles. You can use the existing routes in the web.php file as a starting point to see which endpoints are already available.

  2. Use the api.php file to define your API routes. You can create new routes or modify existing ones to suit your needs. Make sure to use a route prefix that distinguishes the API routes from the web routes, such as api/v1/.

  3. In your Flutter app, use the http package to send HTTP requests to the API endpoints. You can use the get, post, put, or delete methods to send the appropriate request type.

  4. In your Laravel application, use the Auth middleware to protect your API routes and ensure that only authorized users can access them. You can use a token-based authentication system, such as JWT, to secure the API.

  5. Test your API endpoints to make sure they are working as expected. You can use tools like Postman to send requests and debug any issues that arise.

I hope this helps you get started with integrating your Laravel application with your Flutter app using APIs.

CodePudding user response:

$data = ['success' => 1, 'msg' => 'Updated Successfully.'];
return response()->json($data);
  • Related