jeudi 20 avril 2017

Reading URL #hastag in Laravel controller

I have a URL that has a #hashtag on it. For example

http://ift.tt/2pFS60F

In my routes I would have

Route::get('/foo/{loc}', ['uses' => 'FooController@show']);

In my controller I would have

public function show($loc)

What I want to be able to do is read the # value 'tab2' in my controller, but the value of $loc will always be 'location1'. How can I extract the hash value?



via Chebli Mohamed

mercredi 19 avril 2017

Merge two hasOne relation in laravel - Eloquent

public function relationWithId()
{
   return $this->hasOne('App\RelatedProduct', 'product_id', 'id');
}

public function relationWithParent()
{
    return $this->hasOne('App\RelatedProduct', 'product_id', 'parent_id');
}

I have two table one is product and secound is RelatedProduct where related product is connected with products.id and sometime with product.parent_id

so need a third relation where I can merge about both like this

public function relationProduct()
{
   /*****/
}

thanks in advance



via Chebli Mohamed

Entrust on Laravel5.1 - Database Relationship

I followed the documentation and I think the data that has been save into my database is going to be a link and if I click it, I will be redirected in a specific record. Please see the image below.

enter image description here

..The two encircled with red has to be clickable, but it wasn't. Why is that? Please take a look at my code below:

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class EntrustSetupTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return  void
     */
    public function up()
    {
        // Create table for storing roles
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });

        // Create table for associating roles to users (Many-to-Many)
        Schema::create('role_user', function (Blueprint $table) {
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();

            $table->primary(['user_id', 'role_id']);
        });

        Schema::table('role_user', function(Blueprint $table){
            $table->foreign('user_id')->references('id')->on('users')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('roles')
                ->onUpdate('cascade')->onDelete('cascade');
        });

        // Create table for storing permissions
        Schema::create('permissions', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });

        // Create table for associating permissions to roles (Many-to-Many)
        Schema::create('permission_role', function (Blueprint $table) {
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();

            $table->foreign('permission_id')->references('id')->on('permissions')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('roles')
                ->onUpdate('cascade')->onDelete('cascade');

            $table->primary(['permission_id', 'role_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return  void
     */
    public function down()
    {
        Schema::drop('permission_role');
        Schema::drop('permissions');
        Schema::drop('role_user');
        Schema::drop('roles');
    }
}



via Chebli Mohamed

Repeatitive loops within foreach

I have a problem with the looping that iterates three times.Its been hours that i am looking solution why it loops trice.It might be the best idea to find help here to speed up my project.See the sample results below the expected output and the result with incorrect output.Hope anyone could help me here.Thank you in advance.

array:5 [▼
   "1.4" => 40 
   "1.401" => 42 
   "1.5" => 38 
]

array:5 [▼
    "40" => 1.4
    "42" => 1.4
    "38" => 1.5
   ]
    $count1 = 0;

    $percentage = 1.1;
    $last_scholar_gpa = 0;
    foreach ($scho as $scholar_id2) {
    foreach ($all_list_GPA as $scholar_GPA) {
        $scholar = Scholar::find($scholar_id2);
        $crits = New Criteria;
        $crits->scholar_id = $scholar->scholar_id;
        $crits->scholarship_id = $scholarship_id;
            if ($last_scholar_gpa != $scholar_GPA) {
                $percentage = $percentage - 0.1;
                $total_points_two = $scholarship->scholarship_points_two * $percentage;
                $crits->criteria_gpa = $total_points_two;

            $last_scholar_gpa = $scholar_GPA;
            }else{
                $percentage = $percentage;
                $total_points_two = $scholarship->scholarship_points_two * $percentage;
                $crits->criteria_gpa = $total_points_two;

            $last_scholar_gpa = $scholar_GPA;
            }

        $crits->save();
        }   

        $count1++;
    } 

This is supposed to be the result:

id    criteria_gpa
40          25.00
42          25.00
38          22.50

The current result which were saved in the database (but wrong computation)

id    criteria_gpa
40          25.00
40          25.00
40          22.50
42          20.00
42          20.00
42          17.50
38          15.00
38          15.00
38          12.50



via Chebli Mohamed

laravel model factory remove variable name from factory

Can I remove the name from this model factory?

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
    ];
});

because i write unit test and create user in it and i get this error PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'field list' my user model is consist of firstname and lastname



via Chebli Mohamed

Computation inside foreach loop

I have a problem within the loop with correct computation.Right below is the original array which is to flipped.See result of second dd of the array. As you can see,the value is already sorted.Here comes the problem inside the foreach loop.My logic seems confusing even if my expected result is simple. See the following results below.Hope anyone could help me figuring it out the most easy way just come up the expected output.

array:5 [▼
   "1.4" => 40 
   "1.4" => 42 
   "1.5" => 38 
   "1.5" => 39 
   "1.6" => 41 
]

    $count1 = 0;
    foreach ($scho as $scholar_id2) {
        $scholar = Scholar::find($scholar_id2);
        $crits = New Criteria;
        $crits->scholar_id = $scholar->scholar_id;
        $crits->scholarship_id = $scholarship_id;
        $crits->save();
        $count1++;
    }

result of flipped array of index and value

array:5 [▼
    "40" => 1.4
    "42" => 1.4
    "38" => 1.5
    "39" => 1.5
    "41" => 1.6
]
    $list1 = Criteria::where('scholarship_id','=',$scholarship->scholarship_id)->get();
    $all_id = [];
    foreach ($list1 as $id) {
        array_push($all_id, $id->criteria_id);
    }

        $last_scholar_gpa = 0;
        $percentage = 1.2;
        foreach ($all_id as $id2) {
        $crits = Criteria::find($id2);
        foreach ($all_list_GPA as $scholar_GPA) {

        if($percentage > 0.5 && $last_scholar_gpa != $scholar_GPA)
        $percentage = $percentage - 0.1;

        $total_points_two = $scholarship->scholarship_points_two * $percentage;
        $crits->criteria_gpa = $total_points_two;
        }

        $crits->save();

        $last_scholar_gpa = $scholar_GPA;

    }

This is value of scholarship_points_two = 25

This is supposed to be the result:

id    criteria_gpa
40          25.00
42          25.00
38          22.50
39          22.50
41          20.00

The current result which were saved in the database (but wrong computation)

id    criteria_gpa
40          22.50
42          17.50
38          12.50
39          7.50
41          2.50



via Chebli Mohamed

Upload video from s3 to youtube

I want to upload a video from s3 to youtube. Now i am using service account and get access token but when I upload video to youtube server return error:

[Google_Service_Exception]              


{                                       
   "error": {                             
    "errors": [                           
     {                                    
      "domain": "youtube.header",         
      "reason": "youtubeSignupRequired",  
      "message": "Unauthorized",          
      "locationType": "header",           
      "location": "Authorization"         
     }                                    
    ],                                    
    "code": 401,                          
    "message": "Unauthorized"             
   }                                      
  }  

This is my code:

    $googleClient = new Google_Client();
    $googleClient->useApplicationDefaultCredentials();

    $googleClient->setApplicationName('XXX');
    $googleClient->setClientId('XXX');
    $googleClient->setClientSecret('XXXX');
    $googleClient->addScope(\Google_Service_YouTube::YOUTUBE);
    $googleClient->setAccessType('XXX');
    $googleClient->setApprovalPrompt('auto');
    $googleClient->setDeveloperKey('XXX');

    $stringAccessToken = $googleClient->fetchAccessTokenWithAssertion()['access_token'];


    $googleClient->setAccessToken($stringAccessToken);


    $youtube = new \Google_Service_YouTube($googleClient);
....

I don't know Can use service account to upload a video to youtube api V3?

Please help me . thank you so much.



via Chebli Mohamed