vendredi 28 avril 2017

Laravel 5 JWTAuth to check the user role is admin or not

when front-end parse a token to back-end, how to use JwtAuth to check the user relationship(role) that is a admin (role=1) or normal (role=0) ?

any idea?

The relationship with user and role

User

public function role()
{
   return $this->hasOne('FACI\Entity\User\Role','user_id','id');
}

Role

protected $fillable = ['user_id','role'];

public function user()
{
    return $this->belongsTo('FACI\Entity\User','user_id','id');
}



via Chebli Mohamed

How does sessions work in Laravel 5

I am trying to understand how sessions work in Laravel 5(.4). In one hand there are two ways of using them as described in the official documentation:

There are two primary ways of working with session data in Laravel: the global session helper and via a Request instance.

$request->session()->put('key', 'default');

and

session('key', 'default');

The documentation says:

There is little practical difference between using the session via an HTTP request instance versus using the global session helper.

But it is never explained what the difference is.

In the other hand there is the "Facade way":

Session::put('key', 'value');

And recently I found this Stack Overflow question How to use session in laravel 5.2 controller. train_fox pointed out this way:

session()->put('key', 'default');

So that makes a total of four ways. And I cannot figure out why or when use one or another.

By the way, the only way I could get sessions to work with Redis was with the two last ways.

Thank you in advance for your enlightenment.



via Chebli Mohamed

Laravel 5.4 How to update database connection for migration

I have post request where I update database config. I save data for one connection in my storage. In database config I call function to get it.

When data are updated I also update local config by config('database.connections.myconnection',$newConf)

The problem is in artisan:migrate action. In the same request I need to call to artisan:migrate but with new datbaase configuration.

Unfortunately I can set only database string to Artisan::call('migrate',['database'=>'myconnection'])

Migrate try to use old db data and I get error about db connection.

Someone have any idea how I can provide new config for migrate "in fly"?



via Chebli Mohamed

Laravel 5 route url cron execute only by server

I have url for example www.domain.com/cron/

Only the server has rights to execute the url, so nobody can not approach this url by the browser.

How can i achieve that?



via Chebli Mohamed

undefined index on empty array laravel 5

Fellow coders,

It might be a stupid question but I really am stuck on this part in my application.

I am making an hourregistration system for the company i'm an intern at. What I have done is creating a delete button in my application that deletes a record with the click of a simple button. Yet what I did was to delete all the visible records in the table I created that shows all the registrations.

When I deleted the final record I got an error of "undefined index hourregistration".

public function index()
{
    $hoursregistrations = Hoursregistration::latest()->get();
    $user_id = Sentinel::getUser();
    $project_id = Project::pluck('description', 'id');
    $company_name = Company::where('status','client')->pluck('company_name', 'id');
    $activity_name = Subproject::pluck('id');
    //dd($hoursregistrations);

    return view('hoursregistrations.index', compact('hoursregistrations', 'user_id', 'project_id',
    'activity_name', 'company_name'));

}

I think the problem lies at

$hoursregistrations = Hoursregistration::latest()->get();

Because I'm trying to get the latest value of the registration but there is none right?

Now i'm wondering how I could still show my view without breaking my inserting and/or viewing portion of the app.

 @foreach ($hoursregistrations as $hoursregistration)
                <tr>
                   <td hidden></td>
                   <td >{!! App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</td>      
                   <td>{!! $hoursregistration->note !!}</td>    
                   <td>{!! \App\Helpers::dateFormat($hoursregistration->date) !!}</td>
                   <td>{!! $hoursregistration->hours !!}</td>
                   <td>

                    <button id="btn-edit" name="btn-edit" class="btn btn-warning btn-xs btn-detail open-modal" value="">Edit</button>
                    <form action="hoursregistrations//delete" method="POST">
                        
                        
                        <button id="btn-delete" type="submit" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="">Delete</button>
                    </form>
                </td>
            </tr>
            @endforeach

This is the foreach loop that shows the data in the index.blade.php view

I would love some help so I can continue finishing this application



via Chebli Mohamed

jeudi 27 avril 2017

Laragon-can't detect my laravel project

I am trying to learn how to work with Laravel. I successfully installed Laravel through Laragon. The process that I followed-

Menu->Quick Create->Laravel->(Gave a Project name-laravel_CRUD)->Ok.

Then everything was installed and created successfully including database and dependencies were successfully updated. A pretty url was also generated as this format- http://laravel_CRUD.dev. But whenever I pasted this url into my browser's url bar, it didn't work and showed-"The site can't be reached". (Obviously Apache and MySql were started) I stopped those and restart those again. But still it failed to detect my project !!!!! What's wrong with it!!!! Anybody please help.



via Chebli Mohamed

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint Laravel

Im trying to create a foreign keys using artisan, but this error show up.

[Illuminate\Database\QueryException]                                                                                                                                                                             
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `comments` add constraint `comments_comment_lot_id_foreign` foreign key (`comment_lot_id`) references `lots` (`lot_id`  
  ) on delete cascade) 

This is my migration:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->text('comment');
            $table->integer('comment_lot_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('comments', function ($table) {
            $table->foreign('comment_lot_id')->references('lot_id')->on('lots')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropForeign(['comment_lot_id']);
        Schema::dropIfExists('comments');
    }
}

in the lots table i use lot_id as id it model Lot.php i add:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Lot extends Model {
    protected $primaryKey = 'lot_id';

}

Any idea how can i resolve this error?



via Chebli Mohamed