vendredi 27 décembre 2019

DatePicker for Laravel 5.8

I am looking for a recommendation on your datepicker for Laravel 5.8. Currently I am using a pure css datepicker which is ugly. I am having a hard time integrating datepickers for my project because it always indicate

datepicker is not defined

I tried installing this via NPM

https://www.npmjs.com/package/js-datepicker

and implemented this in my js file but I still get the undefined error:

    datepicker(document.querySelector('#date_search'));


via Chebli Mohamed

jeudi 26 décembre 2019

Auth user not added

This code is work fine but when i use auth::id than show error like this in api Message unauthenticated

API

Route::post('/friend', 'FriendController@index')->middleware('auth');

Working code

 public function index(Request $request) {
       
        $sender = Friend::where('sender_id', $request->sender_id)->where('receiver_id',$request->receiver_id)->first();
        if(empty($sender)){
            Friend::create(['sender_id'=>$request->sender_id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);
            
            $response = ['message'=>'Friend Request has been sent','status'=>200];
            
        }else{
            $response = ['message'=>'Request has been sent already','status'=>200];
         
        }
        return response()->json($response);
        
    }

Not working code, error message unauthenticated

public function index(Request $request) {
       //$user = Auth::user()->id;
        $sender = Friend::where('sender_id', $request->Auth::user()->id)->where('receiver_id',$request->receiver_id)->first();
        if(empty($sender)){
            Friend::create(['sender_id'=>$request->Auth::user()->id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);
            
            $response = ['message'=>'Friend Request has been sent','status'=>200];
            
        }else{
            $response = ['message'=>'Request has been sent already','status'=>200];
         
        }
        return response()->json($response);
        
    }

How can i add sender_id is authenticated user id?



via Chebli Mohamed

How to adjust the orientation of a image in order to show proper preview of the image?

I have developed a laravel web application which has a function of accepting images uploaded by users and then display it. I had encountered a problem while testing as photos uploaded using mobile phones were rotating 90 degrees in anti clock wise direction I used image intervention to solve that issue . But as i am showing a preview of the uploaded image to the users using javascript the images are rotated 90 degrees but when i save the image it becomes proper. My javascript code is

    function imagePreview(input,elm) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $(elm).css("background-image","url('"+e.target.result+"')");
            }
            reader.readAsDataURL(input.files[0]);
        }
    }
    $("#settings_img").on("change",function(){
        imagePreview(this,"#settings_img_elm");
    });

can anyone please help me to properly orient the preview image by editing the code above so that the orientation of the image changes when needed.



via Chebli Mohamed

Laravel not fetching result from mongodb

I have a json file called "University.json" which contains only one row of records.

{"University": [{"fees": "$200"}, {"month": "June"}]}

Now I have used mongo import to insert the above *.json file into MongoDb.

mongoimport --db university --collection finance --file University.json 

My Mongo database/collection values are : university/finance To show the values on view blade from MongoDb , I have tried as below but not able to show the return result. Also, I have a top level element in my json file which is "University". So, is this the proper way to fetch the nested elements. Please suggest.

Route (web.php)

Route::group(['middleware' => ['auth']], function () {
     Route::get('/finance', 'FeesController@getFees');
});

Controller (FeesController.php)

...
public function getFees()
    {
        $fees = Finance::all();
        return view('tables', compact('fees'));
    }
...

Model (Finance.php)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;


class Finance extends Eloquent
{
    protected $connection = 'mongodb';
    protected $collection = 'finance';

    protected $fillable = [
        'fees', 'month'
    ];
}

View blade (tables.blade.php)

<tr>
        <th>fees</th>
        <th>Month</th>
    </tr>

      @foreach($fees as $fee)
      <tr>
        <td></td>
        <td></td>
      </tr>
      @endforeach

. env file

MONGODB_CONNECTION=mongodb
MONGODB_HOST=127.0.0.1
MONGODB_PORT=27017
MONGODB_DATABASE=university
MONGODB_USERNAME=
MONGODB_PASSWORD=

database.php

...
    'default' => env('DB_CONNECTION', 'mysql'),
        'mongodb' => [
                    'driver'   => 'mongodb',
                    'host'     => env('MONGODB_HOST', 'localhost'),
                    'port'     => env('MONGODB_PORT', 27017),
                    'database' => env('MONGODB_DATABASE', 'university'),
                    'username' => env('MONGODB_USERNAME'),
                    'password' => env('MONGODB_PASSWORD'),
                    'options'  => [
                        'database' => 'admin' // sets the authentication database required by mongo 3
                    ]
                ],

    'mysql' => [
                'driver' => 'mysql',
                'url' => env('DATABASE_URL'),
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'unix_socket' => env('DB_SOCKET', ''),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'prefix_indexes' => true,
                'strict' => true,
                'engine' => null,
                'options' => extension_loaded('pdo_mysql') ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                ]) : [],
            ],
...

app.php

...
/*
    `Mongo DB
    */
    Jenssegers\Mongodb\MongodbServiceProvider::class,
...

Note : I am using both mysql and mongodb for my requirements. There is no issue while attempting simple json structure without top level element.

Edit (1) :

I have changed the controller function as :

public function getFees()
{
$fees = Finance::where(['University.month' => 'June'])->get();
   return view('tables')->with('fees', json_decode($fees, true));
}

and simply print the below in view blade shows the entire Json structure from mongo.

{!! json_encode($fees) !!}

output :

[{"_id":"5e04a06ca445f78401a16d0a","University":[{"fees":"$200"},{"month":"June"}]}]

But now how to separate the elements ? I want to show Fees = $200 and Month = June in the blade view.

I have tried but not working and giving undefined index exception fees in view.

@foreach($fees['member'] as $member)
    Fees: 
    Month: 
@endforeach


via Chebli Mohamed

right way to pass a boolean from controller to store a form data

i'm new to laravel and don't know what i did wrong. i'm having this "Symfony\Component\Debug\Exception\FatalThrowableError syntax error, unexpected '='" whenever i try to submit my form.

this is in my create.blade.php

<div class="form-check">
  <input class="form-check-input" type="checkbox" value="1" id="#is_available?" name="is_available?">
  <label class="form-check-label" for="is_available?">
    Is this available and ready to be rented?
  </label>
</div>

and in my CarsControllers.php

public function store(Request $request)
{
    $car = new car;

    $car->car_brand = $request->car_brand;
    $car->car_name = $request->car_name;
    $car->description = $request->description;
    $car->car_type_id = $request->car_type_id;
    $car->image_location = '';
    $car->is_available? = $request->is_available?;


    $car->save();

    return redirect('/selections');

}

The rest is okay except with that boolean. i don't know what to do. my column name is 'is_available?' to i have to change it?



via Chebli Mohamed

Get user data using passport access token - Laravel

I've been trying to return user data using access token but keep getting error:

Invalid payload

My method was to get the token then find the user id from oauth_access_tokens table. My code is as follows:

public function authenticateUser($token){
     $user_id = DB::table('oauth_access_tokens')->where('id', trim($token))->value('user_id');
     $user = \App\User::find($user_id);

     Auth::login($user, true);
}

The token is something like this:

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjkyZGU3ZGYyMDcxZjgzMzU5YWUxMmRlYzM4ZGJiM2EyMTk0NzEyYTQ5NmRiNzgwZWJkMDg2Yjc0NThkZjU0NmFlZmU2Yzg0N2Q0Mjc5MDAxIn0.eyJhdWQiOiIxIiwianRpIjoiOTJkZTdkZjIwNzFmODMzNTlhZTEyZGVjMzhkYmIzYTIxOTQ3MTJhNDk2ZGI3ODBlYmQwODZiNzQ1OGRmNTQ2YWVmZTZjODQ3ZDQyNzkwMDEiLCJpYXQiOjE1NzczNzE4MDYsIm5iZiI6MTU3NzM3MTgwNiwiZXhwIjoxNjA4OTk0MjA1LCJzdWIiOiIzMCIsInNjb3BlcyI6W119.Io4xkJYEczbI7rhFD_UKAoe7v_1-RLJXjA6XqGIe2nRAWEgMkg-mokQUiGz41xYVazmDmACDwwYSRr-iTTzwc591NABfxsmMk7OdYkUKb93UTA3JhKClEGSP82y1QrIfm9XTZ0KKDaCKlfKqye1Aobj9zFthQdApegTaK61ReLQa7MzO6EM5fcZ3udsLL3QpKXFuyO6JcPKRauKIbA8oNIKEdadprLWJSeQieIyA8lpYOr453QzgZGgzCwPY1U2RmIbCzqyNQD_L5264-ix1503KxgPt4F_Cl82WXm7tNsZKNwE-vGKhCc2CcgAgTV1lIj7ItDf2KpDh_Jt96Uiv2eJ3OtXYvuOTErz9mNnQ1T38hxQmKDh8XlG3f7JgIWWzN6m8ItBV1KyGZi0-vn2HXetkZTNIyfJV8E5-RaGUzIKX7RejWd5BVaqFw0OjDYPeliVOaZzfcZCRnPDSJBGwf7YqJrRXP61LMasn_ZJ-i8G5JIaQx2vdmfYgE41O5F9fE5uEF5-mIV979RbnswL6CJsSGmmUMzC7mPhqL6HtPu2hMTnfHbKY0-efqtzZ5I2TBQU6ODM37RFN5TEljoEgBFG6kAImkGDy4QFH5uqt6V7-ZFxvrKQzQozgezSgA6ITF1sRb7yWfI-9rF7sYE_aKu3r1_KRr4UJLoZqFyvGPP0

Isn't it the token that I should pass to the function above. When I pass it to base64_decode, I see the JSON object along with other gibberish. What am I doing wrong here?



via Chebli Mohamed

How to deal images in @vue/cli app with Laravel 5 Backend?

I make @vue/cli 4.0.5 / vuex 3 app with data reading from Laravel 5 Backend REST API and for this in routes I search how to make images support functionality: 1) to upload image from vue client 2) upload it to server. ( In subdirs of frontend or backend preferable ?) 3) assign any image with item in db 4) when some item data are read from REST API to give ref to this image(full or relative preferable ?)

Thanks!



via Chebli Mohamed