samedi 2 avril 2016

Append value to existing session Laravel 5

Once in my login I set values of user to session like this..

session()->set("user", $user);

And for get the values of the session from anywhere in my application attach with it to $request $request->_user = $user; like this. So I can get it like this from anywhere

$user = $request->_user;

Thing is I want pass some ID of purchase order at the midle of the application. So can I attach it to existing session or how to create new session and append the ID. With that ID, page redirect to paypal and If user cancel the payment I can catch his canceled payment with that ID. That is why I thought session would help me. After a successful payment it is OK to destroy that session. Help me !!!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Su2LSh
via IFTTT

vendredi 1 avril 2016

Returning FatalErrorException when defining roles and permissions on laravel

I have three models that are related. First I have User that belongs to a Role. On the other hand, Role has many roles. Role belongs to many permissions and Permissions belongs to many Role. I am using the AuthServiceProvider as suggested by jeffrey way of laracast. But the problem now, when I want to fetch all the permissions of a User I am having error which is, "Call to a member function getKey() on boolean". Can someone please help me on this. Please refer to the codes below.

User.php

public function role()
{
    return $this->belongsTo('App\Role');
}

public function assignRole($role)
{
    return $this->roles()->save(
        Role::whereName($role)->firstOrFail()
    );
}

public function hasRole($role)
{
    if(is_string($role)){
        return $this->role->contains('name', $role);
    }

    return !! $role->intersect($this->role);

}

Role.php

class Role extends Model
{
    public function users()
    {
        return $this->hasMany('App\User');
    }

    public function permissions()
    {
        return $this->belongsToMany('App\Permission');
    }

    public function givePermissions(Permission $permission)
    {
        return $this->permissions()->save($permission);
    }
}

Permission.php

class Permission extends Model
{
    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }
}

AuthServiceProvider

public function boot(GateContract $gate)
{
    $this->registerPolicies($gate);

    //get all permissions
    foreach ($this->getPermissionTo() as $permission ) {

        $gate->define($permission->name, function($user) use ($permission){
            return $user->hasRole($permission->roles);
        });
    }
}

public function getPermissionTo()
{
    return Permission::with('roles')->get();
}

and lastly, heres the user table that has a foreign key of role_id

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('role_id')->unsigned();

        $table->string('id_no')->unique()->index();
        $table->string('u_first_name');
        $table->string('u_middle_name');
        $table->string('u_last_name');
        $table->string('email')->unique();
        $table->string('password');

        $table->rememberToken();
        $table->timestamps();
    });



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1RDxZqi
via IFTTT

push for loop result as array laravel

I know this easy but I still can't solve it, I have this piece of code :

$names = [
    '1' => 'name1',
    '2' => 'name2',
    '3' => 'name3',
];  

It's simple for small number array, but how if i get many data and always change?, I plan to use for loop

$totaldata = 5    

for($z=1; $z<=$totaldata; $z++) {
    $yz = name.$z;

    $names = [
        $z => $yz,
    ];
}

but somehow it doesn't work, any solution?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1X5hDdO
via IFTTT

Is there a better way to assign an Eloquent relationship in Laravel using a Repository?

Assuming I have these 2 Models User and Account.
Where a User can have multiple Accounts. (hasMany)
And an Account belongs to a User. (belongsTo)

If I'm not using a Repository I would save the model and its relationship like this:

    $account = new Account();
    $account->username = $this->username;
    $account->secret = $this->secret;
    $account->user()->associate($this->user);
    $account->save();

And when using a Repository I see many people (and articles) doing it like this:

$accountRepository->create([
      'username' => $this->username,
      'secret'   => $this->secret,
      'user_id'  => $this->user()->id,
]);

My problem with this approach is in the user_id because it doesn't feel safe to manually assign this kind of relationship, additionally when someone else is reading this code he cannot tell what is the relation between the Account and User from there!!

The way I'm currently handling this is as follow: (it's kinda combination of the both ways)

    // create the account and attach the fields and the relationships
    $account = new Account();
    $account->username = $this->username;
    $account->secret = $this->secret;
    $account->user()->associate($this->user);

    // save the new created model with the repository
    $account = $accountRepository->create($account->toArray());

But I'm not really sure if there's any better way to do this!! All I need is to use the associate function because it feels safer and looks better for the readers. And to use a Repository to save and access my data.

Note: I am using the http://ift.tt/1BVSE6r to abstract the database layer.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1RSrBAN
via IFTTT

Errors not displaying on View. Laravel-5

In my Laravel app I did some Validation of a form in a controller using the request variable. The validation seems to work, however when I try to display the errors in the view they are not showing up. I know the validation is working because as soon as I type something that is not a integer it brings me back to the view with the form. Here is the view with the form: (The 'if' statement that displays errorr is not working):

@if(count($errors) > 0)
  <ul>
      @foreach ($errors->all() as $error)
          <li>{{ $error }}</li>
      @endforeach
  </ul>
@endif
<form method='POST' action='/lorem/show'>



<label>Enter Number of Paragraphs</label>
<input type='hidden' name='_token' value='{{ csrf_token() }}'>
<input type='text'
        id='paragraph'
        name='paragraph'
        maxlength="2"
        size="5"
        <br/>

<input type="checkbox" name="header" value="header"/>Include Headers<br/><br/>
<button type="submit" class="btn btn-primary">
&nbsp;Create </button>
</form>

And here is the function in my controller:

public function postShow (Request $request) {

  $this->validate ($request, [
  'paragraph' => 'required|integer',
 ]);
}

Why are the errors not showing up in the view?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1UJAOfB
via IFTTT

Laravel 5.0, migration: how to make integer not a primary key?

I would like to migrate a table with the elements below.

public function up() {
    Schema::create('users', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('LoginID', 9)->unsigned();
        $table->string('username');
        $table->string('email')->unique();
        $table->string('password', 60)->unique();
        $table->rememberToken();
        $table->timestamps();
    });
}

However, I have kept dealing with the error below. Does anyone know how to make integer "LoginID not a primary key so that I can migrate the table below? Any advice appreciated. Thanks in advance.

[Illuminate\Database\QueryException]                                                                               
  SQLSTATE[HY000]: General error: 1 table "users" has more than one primary key (SQL: create table "users" ("id" integer not null primary key autoincrement, "LoginID" integer not null primary key autoincrement, "username" varchar not null, "email" varchar not null, "password" varchar not null, "remember_token" varchar null, "created_at" date time not null, "updated_at" datetime not null))    



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1VhmWYw
via IFTTT

I am getting error massage

Getting error : syntax error, unexpected '=>' (T_DOUBLE_ARROW) my code is :

 @extends('layouts.masters.main')
        @section('page-content')

        <div class="container">

         @include('layouts.partials.nav')

         {!! Form::open(['route' => 'post_register', 'id' => 'registration-form']) !!}

             {!! Form::label('name', 'Full Name') !!}
        {!! Form::text('name', null, ['id' => 'name', 'class' => 'form-control', 'placeholder' => 'Full Name', 'required']) !!}

         {!! Form::label('email', 'Email Address') !!}
         {!! Form::email('email',null,['id' => 'email', 'class' => 'form-control', 'placeholder' => 'Email Address', 'required']) !!}


         {!! Form::label('password', 'password') !!}
         {!! Form::password('password',['id' => 'password', 'class' => 'form-control', 'placeholder' => 'password', 'required']) !!}

         {!! Form::button('Register','class' => 'btn btn-lg btn-primary btn-block', 'type' => 'submit')!!}
         {!! Form::close() !!}


        </div> <!-- /container -->
        @stop

please help ... i can't uderstand where i do the mistake in this code



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/21XtaNe
via IFTTT