mardi 30 janvier 2018

What is the best way to check active status for user whille loging in native multi-auth Laravel?

I am using laravel multi-auth. I have a table column called status. At the time of login in, I want compare user whether it is active or in active. If active only login and if not give a message 'inactive account, please contacct to administrator'.

Here is my login controller.

    <?php
    namespace Modules\University\Http\Controllers;  

    class LoginController extends Controller
    {
        protected $redirectTo = '/university';

        public function __construct()
        {
            $this->middleware('guest:university', ['except' => ['universityLogout']]);
        }

        public function showLoginForm()
        {

            return view('university::login');
        }

        public function login(Request $request)
        {
            $this->validate($request, [
                'email' => 'required|email',
                'password' => 'required|min:6',
            ]);

            //attempt to log the user in
            if(Auth::guard('university')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){

/****Here I want check whether university status is active or not and give message if not else login*****/

                return redirect()->intended(route('university.dashboard'));
            }


            Session::flash('failed', 'Login credential incorrect!');
            return redirect()
                ->back()
                ->withInput($request->only('email', 'remember'));
        }

        public function universityLogout(Request $request)
        {
            Auth::guard('university')->logout();
            return redirect(route('university.login'));
        }
    }

Thanks in advance.



via Chebli Mohamed

Vue 2 Using external variables inside Instance

I am working on a Laravel 5.5 & Vue 2.x project which requires to define some data in the "front-end" and use them in the Vue Instance:

First, I have my view or HTML file:

<fieldset class="form-group col-md">
    <label for="country" class="control-label">Country</label>
    <select class="form-control" name="country" id="country" v-model="selectedCountry" @change="selectCountry()">
        <option :value="null" :selected="selectedCountry">Select one</option>
        <option v-for="country in countries" :value="country.id">@</option>
    </select>
</fieldset>

<script>
    var formSelectedCountry = ;
    var formSelectedDepartment = ;
    var formSelectedLocation = ;
    var formSelectedZone = ;
</script>

This works fine. But in other pages where formSelectedCountry (for instance) is not defined Vue seems not to work.

I have this in my app.js:

data: {
    ...
    selectedCountry: formSelectedCountry,
    selectedDepartment: formSelectedDepartment,
    selectedLocation: formSelectedLocation,
    selectedZone:  formSelectedZone,
    ...
}

I have also tried to prepend these lines in order to "pre-define" them:

formSelectedCountry: '',
formSelectedDepartment: '',
formSelectedLocation: '',
formSelectedZone: '',

Or maybe there is a way to set the data with selectedCountry: (formSelectedCountry ? formSelectedCountry : null.

Any ideas are appreciated.



via Chebli Mohamed

PusherError App key does not exist in this cluster

I am trying to user pusher in laravel 5.4 in xampp with php 5.6.4

"vinkla/pusher": "^2.3"

But getting error as App key does not exist in this cluster

Error

{"type":"WebSocketError","error":{"type":"PusherError","data":{"code":4001,"message":"Did you forget to specify the cluster when creating the Pusher instance? App key 04f5869e102fa2eb9902 does not exist in this cluster."}}}

broadcasting.php

'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
              'cluster' => env('PUSHER_APP_CLUSTER'),
              'encrypted' => true,
            ],
        ]

env

BROADCAST_DRIVER=pusher
PUSHER_APP_ID=
PUSHER_APP_KEY=04f5869e102fa2eb9902 
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=ap2

Pusher : Connecting : {"transport":"ws","url":"wss://ws.pusherapp.com:443/app/04f5869e102fa2eb9902?protocol=7&client=js&version=3.1.0&flash=false"}



via Chebli Mohamed

How create service provider (singleton)?

I have such function which create new connection when I call it:

public function newBaseQueryBuilder()
{
   ...
   $host = config('sphinx.connection.host');
   $port = config('sphinx.connection.port');
   $pdo = new \PDO("mysql:host={$host};port={$port};", '', '');
   $connection = new SphinxConnection($pdo);
   ...
}

Have create service provider which create only one connection?



via Chebli Mohamed

Get all related articles from given article using eager loading

I want to get all related articles of particular article based on category.

I have relation between Article and Category

Article.php

public function category(){

    return $this->belongsTo(ArticleCategory::class,'category_id','id');

}

ArticleCategory.php

 public function articles(){

    return $this->hasMany(Article::class,'category_id','id');

}

ArticleController.php

  public function singleArticle(Article $article){

// I want to convert this statement to eager loading statement

    $relatedArticles = $article->category->articles;

    return view('pages/article',compact('article','relatedArticles'));


}



via Chebli Mohamed

LARAVEL 5.5 - Insert Data into Database

I have a problem with inserting Data into Database.

All i have done till now is :

Create a Model with Controller and Migration via:

php artisan make:model Cars -mcr


So, now all of my files looks this way :

Cars - Model

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Cars extends Model
{

}

AddCar.blade.php - View

<form action="" method="post">
    
    <input type="text" name="brand" placeholder="Marka">
    <input type="text" name="model" placeholder="Model">
    <input type="text" name="doors" placeholder="Ilość drzwi">
    <input type="text" name="priceHour" placeholder="Cena za godzinę">
    <input type="text" name="priceDay" placeholder="Cena za dzień">
    <input type="text" name="priceWeek" placeholder="Cena za tydzień">
    <input type="text" name="priceMonth" placeholder="Cena za miesiąc">
    <input type="text" name="priceYear" placeholder="Cena za rok">
    <input type="submit" value="Osadź">

</form>

CarsController - Controller

namespace App\Http\Controllers;

use App\Cars;
use Illuminate\Http\Request;

class CarsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return "test";
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $cars = new Cars;
        $cars->brand = $request->input('brand');
        $cars->brand = $request->input('model');
        $cars->brand = $request->input('type');
        $cars->brand = $request->input('doors');
        $cars->priceHour = $request->input('priceHour');
        $cars->priceDay = $request->input('priceDay');
        $cars->priceWeek = $request->input('priceWeek');
        $cars->priceMonth = $request->input('priceMonth');
        $cars->priceYear = $request->input('priceYear');
        $cars->save();
        return redirect('admin.AddCar');
    }

web.php - Routing

Route::resource('/cars', 'CarsController');


Error I receiving after fill out all fields and click "Osadz" = submit is:

SQLSTATE[HY000]: General error: 1364 Field 'model' doesn't have a default value (SQL: insert into cars (brand, priceHour, priceDay, priceWeek, priceMonth, priceYear, updated_at, created_at) values (3, 3, 53, 3, 35, 3, 2018-01-30 09:36:57, 2018-01-30 09:36:57))



And my question is, what default value is missing in my code ?



via Chebli Mohamed

laravel collection/format printing |Property [id] does not exist on this collection instance

  $Report = Db::table('Attendance_Processed as procc')                            
                      ->leftjoin('Emp_Details', 'Emp_Details.HRApp_Emp_Id', '=', 'procc.HRApp_Emp_Id')
                            ->where('procc.Org_ID',$orgid)
                            ->where('Emp_Details.Supervisor_Emp_ID',$hrid)
                            ->whereYear('Clock_Day',$yearofdata)
                            ->whereMonth('Clock_Day',$monthofdata)   
                            ->select('Emp_Details.Emp_First_Name as name','procc.HRApp_Emp_Id as hrid',
                              'procc.Clock_Day as clockedday',
                              'procc.Time_in as timein',
                              'procc.Time_out as Timeout',
                              'procc.Total_Work_Hrs as totalworkedhours')


                           ->get()->groupBy('hrid','clockedday');
                           dd($Report);
                             return $Report;

the following code produce the this format

> Collection {#1168 ▼   #items: array:13 [▼
>     8 => Collection {#1155 ▼
>       #items: array:6 [▼
>         0 => {#934 ▼
>           +"name": "Aditi"
>           +"hrid": "8"
>           +"clockedday": "2017-10-03"
>           +"timein": "07:59:37"
>           +"Timeout": "16:52:30"
>           +"totalworkedhours": "08:52:53"
>         }
>         1 => {#935 ▼
>           +"name": "Aditi"
>           +"hrid": "8"
>           +"clockedday": "2017-10-04"
>           +"timein": "07:33:59"
>           +"Timeout": "16:31:48"
>           +"totalworkedhours": "08:57:49"
>         }
>         2 => {#936 ▶}
>         3 => {#937 ▶}
>         4 => {#938 ▶}
>         5 => {#939 ▶}
>       ]
>     }
>     20 => Collection {#1156 ▶}
>     213 => Collection {#1157 ▶}
>     11 => Collection {#1158 ▶}
>     22 => Collection {#1159 ▶}
>     10 => Collection {#1160 ▶}
>     9 => Collection {#1161 ▶}
>     28 => Collection {#1162 ▶}
>     29 => Collection {#1163 ▶}
>     16 => Collection {#1164 ▶}
>     17 => Collection {#1165 ▶}
>     25 => Collection {#1166 ▶}
>     31 => Collection {#1167 ▶}   ]

I am not able to print it properly on blade with foreachloop.

when i am trying to print it in blade file its showing the following error


"Property [hrid] does not exist on this collection instance".


please help me in this ..

Thank you in advance



via Chebli Mohamed