dimanche 22 mai 2016

Laravel 5.1 - Filter more one data with relationship

i have a problem to show my products that have a specific category AND specific gender, this is my tables migration and models: Products migration:

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->string('slug'); 
            $table->text('description');
            $table->string('extract', 300);
            $table->decimal('price', 5, 2);
            $table->string('image', 300);
            $table->boolean('visible');
            $table->integer('user_id')->unsigned();

            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');

            $table->integer('category_id')->unsigned();

            // relations  
            $table->foreign('category_id')
                  ->references('id')
                  ->on('categories')
                  ->onDelete('cascade');
            $table->integer('gender_id')->unsigned();


            $table->foreign('gender_id')
              ->references('id')
              ->on('genders')
              ->onDelete('cascade');

            $table->timestamps();

model product

<?php

namespace dixard;

use Illuminate\Database\Eloquent\Model;

use dixard\User;

use dixard\Category;

use dixard\Gender;

use dixard\OrderItem;

use dixard\Color;

class Product extends Model
{

    protected $table = 'products';

    protected $fillable = 

    [
    'name',
    'slug',
    'description',
    'extract',
    'image',
    'visible',
    'price',
    'category_id',
    'gender_id',
    'user_id'

    ];



    public function user() {
            return $this->belongsTo('dixard\User');


    }

    public function category() {
            return $this->belongsTo('dixard\Category');

    }

    public function gender() {
        return $this->belongsTo('dixard\Gender');
    }



    public function OrderItem() {
            return $this->belongsTo('dixard\OrderItem');

    }


    public function Color() {
            return $this->belongsTo('dixard\Color');

    }

}

Category migrations

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255)->unique();

            $table->string('slug');
            $table->text('description');

            $table->string('color', 30);


        });

Model category

use dixard\Product;

class Category extends Model
{
    protected $table = 'categories';

    protected $fillable = [

    'name',
    'slug',
    'description',
    'color',


    ];
    public $timestamps = false;

    public function products() {

        return $this->hasMany('dixard\Product');

    }

}

Gender migrations

<?php

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

class CreateGendersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('genders', function (Blueprint $table) {
            $table->increments('id');

            $table->text('gender');


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

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

Gender Model

<?php

namespace dixard;

use Illuminate\Database\Eloquent\Model;

use dixard\Product;

class Gender extends Model
{
    protected $table = 'genders';


    // gli dico che voglio scrivere questo campi
    protected $fillable = [

    'gender',


    ];
    public $timestamps = false;

    public function products() {

        return $this->hasMany('dixard\Product');

    }
}

I'm trying to show all products that have category_id = 1 (t-shirt category) AND gender_id = 2 (Woman gender), but i have some problem, this is my controller:

use dixard\Product;
use dixard\Category;
use dixard\Gender;

class TshirtController extends Controller
{

       public function men_tshirt()
        { 
            $category = Category::where('name', '=', 't-shirt')->orderBy('id', 'desc')->first();
            $gender = Gender::where('gender', '=', 'woman')->orderBy('id', 'desc')->first();

            $products = $category->products();
            return view('store.shop.men',compact('products'));

            // It filter only category and not gender woman

        }



via Chebli Mohamed

Access files in storage folder only through Auth Middleware and Token based authentication

I have following folder in my Laravel website.

/storage/Asset/Media

This folder can have info like below

/storage/Asset/Media/1/abc.png

/storage/Asset/Media/2/abc.png

Here 1 or 2 is the folder names.

I have following code to secure the folder so that nobody can access the folder without authentication

Route::group(['middleware' => ['web', 'auth']], function () {
    Route::get('/storage/Asset/Media/{ID}/{eded}', array(
        'as' => 'Files',
        'uses' => 'User\Account\Media\MediaController@DownloadMedia',
    ));
});

so in this way nobody can access the files until user's session is not expired in a browser.

Issue is in Android, so now nobody can access the files due to Auth Middleware.

Can somebody suggest the approach such that, files can be accessible only via Token Based Authentication and also using Auth Controller?



via Chebli Mohamed

Token based authentication for API in Laravel 5.2.31

I have two database tables in MySQL.

  1. User Table
  2. Class Table

I have a working code in Laravel 5.2 on localhost, where I can see Class screen only after authentication. So far everything is working fine.

Now, I have to access the list of class, Add/Update Class functions from Android App.

I am thinking to use Token Based authentication. I saw the User table that Laravel provides when we create a new project. We have remember_token in User Table. I was thinking to use same token for Android and for Website.

Here the problem is : Token will be expired if you logout from the website and if I use same token, then expired token can not be used in android requests even if Android has done the authentication already.

Please suggest the correct way to use Token based authentication



via Chebli Mohamed

samedi 21 mai 2016

Laravel 5.1 - view data filter with relationship

i have a problem to show my products that have a specific category, this is my tables migration and models: Products migration:

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->string('slug'); 
            $table->text('description');
            $table->string('extract', 300);
            $table->decimal('price', 5, 2);
            $table->string('image', 300);
            $table->boolean('visible');
            $table->integer('user_id')->unsigned();

            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');

            $table->integer('category_id')->unsigned();

            // relations  
            $table->foreign('category_id')
                  ->references('id')
                  ->on('categories')
                  ->onDelete('cascade');

            $table->timestamps();

model product

<?php

namespace dixard;

use Illuminate\Database\Eloquent\Model;

use dixard\User;

use dixard\Category;

use dixard\OrderItem;

use dixard\Color;

class Product extends Model
{

    protected $table = 'products';

    protected $fillable = 

    [
    'name',
    'slug',
    'description',
    'extract',
    'image',
    'visible',
    'price',
    'category_id',
    'user_id'

    ];



    public function user() {
            return $this->belongsTo('dixard\User');


    }

    public function category() {
            return $this->belongsTo('dixard\Category');

    }

    public function OrderItem() {
            return $this->belongsTo('dixard\OrderItem');

    }


    public function Color() {
            return $this->belongsTo('dixard\Color');

    }

}

Category migrations

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255)->unique();

            $table->string('slug');
            $table->text('description');

            $table->string('color', 30);

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

Model category

use dixard\Product;

class Category extends Model
{
    protected $table = 'categories';

    protected $fillable = [

    'name',
    'slug',
    'description',
    'color',


    ];
    public $timestamps = false;

    public function products() {

        return $this->hasMany('dixard\Product');

    }

}

I'm trying to show all products that have category_id = 1, this category id=1 is my t-shirt category. My controller:

use dixard\Product;
use dixard\Category;

class TshirtController extends Controller
{

       public function men_tshirt()
        { 
            $category = Category::where('name', '=', 't-shirt')->orderBy('id', 'desc')->first();
            $product_men = Product::where('category_id','=', $category->id)->orderBy('id', 'desc');

            dd($product_man) OR
            return view('store.shop.men',compact('product_men'));

            // It doesnt work, doesnt show me nothing.

        }



via Chebli Mohamed

Bulk Insert instead of foreach loop: Laravel 5.2.31

I am using the following code to insert the records One by One.

foreach($data["SportsTypes"] as $SportsTypeID) {
    $userSportsType = new \App\Models\User\UserSportsTypeModel();
    $userSportsType->UserID       = 1;
    $userSportsType->SportsTypeID = $SportsTypeID;
    $userSportsType->save();
}

Can I do bulk insert so as to get rid of saving process in iteration?



via Chebli Mohamed

Trying to bulk update instead of one by one in Laravel 5.2.31

I am using the following code to check all active memberships of the user

$UserMemberships = \App\Models\User\Membership\UserMembershipModel
::where('UserID', $UserID)
->where('IsActive', true)
->get();

Then I am setting the active statue of membership to false one by one.

foreach($UserMemberships as $UserMembership) {
    $UserMembership->IsActive = false;
    $UserMembership->save();
}

Is there any way to do it in one shot?



via Chebli Mohamed

vendredi 20 mai 2016

Read data from Json when passing from view to controller in PHP

I have a problem when i passing data from view to controller in laravel 5 In controller, i receive is:

array(2) {
  ["usercode"]=>
  string(72) "
                                SK20160005
                            "
  ["password"]=>
  string(70) "
                                oepyjrhy
                            "
}

i have try to read data from it, but not success. Someone can help me. Thanks so much.



via Chebli Mohamed