mercredi 28 octobre 2015

Laravel5 pagination with Jquery Ajax

I am using Laravel5 pagination. I have added some ajax code because when I click next button I don't need to refresh the page.

My issue is when I am clicking next button my page is refreshing. please help me to find this issue. My code and screen shot are given below.

In firefox firebug console showing some error "TypeError: Argument 1 of Window.getDefaultComputedStyle is not an object."

js #abilitytest: Click function for showing data in bootstrap model

$( "#abilitytest" ).click(function() {
    $.post("/abilitytest", function(response){
        $( "#abilityQuestions" ).html(response);
    });
});


$('.pagination a').on('click', function (event) {
    event.preventDefault();
    var page = $(this).attr('href');
    console.log(page);
    alert('hai');
});

enter image description here



via Chebli Mohamed

Relationship Has Many Through without firstKey

is there anyway to get result from hasManythrough relationship without the firstKey?

Right now my result is:

 return $this->hasManyThrough('App\Facility', 'App\MerchantBranchFacility', 'id,'facility_id')->select('name');

    {"facilities":[{"name":"AC","id":"13"},{"name":"Wi-Fi","id":"13"}}}

I realize that inside hasmanythrough method laravel always do this:

  return array_merge($columns, [$this->parent->getTable().'.'.$this->firstKey]);

How can i remove "id" with laravel way?



via Chebli Mohamed

Laravel 5 Carbon Unexpected data

I created new date field called start_date, and I also have two columns for created_at and updated_at which are defined as timestamp.

In my model I have following line, that should tell Laravel to treat these columns as Carbon objects.

protected $dates = ['created_at', 'updated_at', 'start_date'].

Request passes me date like this: 2015-10-28T10:37:31.337Z, and when I try to save it, I got following error:

InvalidArgumentException in Carbon.php line 414:
Unexpected data found.
Trailing data

in Carbon.php line 414
at Carbon::createFromFormat('Y-m-d H:i:s', '2015-10-28T10:37:31.337Z') in Model.php line 2925

I tried several things, including changing dateFormat on Model, changing format on property before saving model, but I keep receiving same error.

What is supposed way to handle situations like this? Can I have different types of columns handled by Carbon? Do I need to change it change format for them manually? I checked docs, but I couldn't find anything regarding that.



via Chebli Mohamed

Laravel 5: TokenMismatchException while sending post ajax on mobile

I see sometimes in logs TokenMismatchException and I noticed that this exception is thrown only for mobile users (Android, iOS) or Google Bot.

I set in meta tag csrf token and when the page is loaded I make a post ajax request. I set header in that request like that:

 $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
 });

but this token is other that the one saved on the session.

Unforunately I would be able only once to reproduce this error myself on iPhone. When I loaded the page again everything was ok (even when I deleted all browser data).

Does anyone know what is wrong?



via Chebli Mohamed

Check if Password exists

I'm using laravel 5.1. Now I want to retrieve check if a certain password is already defined in the database. Here's my database schema

/* Accounts table */

Schema::create('accounts', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('pharmacist_id')->unsigned()->index();
      $table->foreign('pharmacist_id')->references('id')->on('pharmacists')->onDelete('cascade');
      $table->string('username')->unique();
      $table->string('password', 70);
      $table->string('rights');
      $table->rememberToken()
      $table->timestamps();
});

/* Pharmacists Table */

Schema::create('pharmacists', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('pharmacy_id')->unsigned()->index();
      $table->foreign('pharmacy_id')->references('id')->on('pharmacies')->onDelete('cascade');
      $table->string('fname');
      $table->string('mname');
      $table->string('lname')
      $table->date('bdate');
      $table->string('email');
      $table->string('contact');
      $table->timestamps();
});

Now what I want is to check if a certain password is already defined in a certain pharmacy_id it looks something like this

$accounts = Account::whereHas('pharmacist', function($query) {
                    return $query->where('pharmacy_id', Auth::user()->id);
                })->where('password', $password)->get();

But it seems that the password is only being passed as a plain text and not encrypted. Also I tried using this method

where('password', bcrypt($password))
where('password', Hash::make($password))
where('password', Crypt::encrypt($password))

But none of this works. Any solution guys? I'm thinking of something like this and I'm not sure if this is possible

$is_valid = Auth::validate(array('pharmacist.pharmacy_id' => Auth::user()->id, 'password' => $value));

Because if I used the below code I can able to check if the user has inputted the valid password.

$is_valid = Auth::validate(array('id' => Auth::user()->id, 'password' => $value));

It's easy to check if the username and password match using the Auth::validate but the needed checking is to check if a certain pharmacist already inputted this specific password. So basically its kinda like looping in all the accounts and check if their password is the same as this specific password.

Here's what I have so far but this has some problem. If a certain pharmacy has 1000+ user then this will loop 1000x which is not optimized and not a good solution

$accounts = Account::whereHas('pharmacist', function($query) {
                    return $query->where('pharmacy_id', Auth::user()->id);
                })->get();


foreach($accounts as $account) {
    if (Hash::check($value, $account->password)) {
      // FOUND!!!
    }
}

To make it short

Pharmacy has many Pharmacists Pharmacist has one account

Now I want to check if a certain Pharmacy has an account password of "certain password" so its like I need to check all account belonging to a certain pharmacists and that pharmacy belongs to a certain pharmacy



via Chebli Mohamed

laravel with codeception unitTesting : can't check relation

I want to unittest hasMany belongsTo&more method.

environment 

  • laravel 5.0.33
  • codeception 2.0.16

example
app/User.php

public function user()
{
    return $this->hasMany('\App\Users', 'name', 'user');
}

what would be a good way to do it?



via Chebli Mohamed

mardi 27 octobre 2015

Cannot send email using Laravel 5

I wrote this code a couple of months ago. I used to be able to send email with the following configuration. But suddenly today, I am getting the following error.

Swift_TransportException in StreamBuffer.php line 265:

Connection could not be established with host localhost [Connection timed out #110]

This is the configuration from which I was able to send emails. Please help me. I am really confused?

'driver' => 'smtp',

'host' => 'localhost',

'port' => 587,

'from' => ['address' => "test@xxx.com", 'name' => "test"],

'encryption' => 'tls',

'username' => 'test@xxx.com',

'password' => '*********',

'sendmail' => '/usr/sbin/sendmail -bs',

'pretend' => false,



via Chebli Mohamed