jeudi 23 juin 2016

How to automate Google SiteVerification Service using laravel in back-end and angularjs in front-end?

I have created the api for the execution of a process triggered from the front-end performing other functionalities (suppose function site(param1, param2) : param1-> {id} , param2->Request object). Then in that function site() , I have called the function of same controller for the google services (suppose function google(param1): param1->Request object) in that I have called the custom service designed to perform google Site Verification. Given the redirect uri in google client json is of function google().

Problem I am facing from calling function site() from front-end is that it is unable to perform google services.



via Chebli Mohamed

Laravel 5.1 using morph relation with global scope

I have global scope on my Product model and method withChildren to get data over scope. All was ok, until i tried use it with morph relation.

Code

Scope code

public function apply(Builder $builder, Model $model)
{
    return $builder->whereNull('parent_id');
}

/**
 * Remove the scope from the given Eloquent query builder.
 *
 * @param  \Illuminate\Database\Eloquent\Builder  $builder
 * @param  \Illuminate\Database\Eloquent\Model  $model
 * @return void
 */
public function remove(Builder $builder, Model $model)
{
    $query = $builder->getQuery();
    foreach ((array) $query->wheres as $key => $where)
    {
        if($where['column'] === 'parent_id')
        {
            unset($query->wheres[$key]);
            $query->wheres = array_values($query->wheres);
        }
    }
}

withChildren method

public function scopeWithChildren()
{
    return with(new static)->newQueryWithoutScope(new ParentScope);
}

Scope injected in model through boot method, like so

protected static function boot()
{
    parent::boot();
    //exclude children products from all results by default
    Product::addGlobalScope(new ParentScope);
}

Problem

Relation returns null before i can implement my withChildren method. Invoice and Product have simple plymorphic relation.

$products = $invoice->products; //products is null, because of global scope

Tried

$invoice->products()->withChildren()->get() //500 error without any description
$invoice->with('products', function($q) {$e->withChildren();})->get(); //explode() expects parameter 2 to be string, object given



via Chebli Mohamed

Send email when make new post in laravel

I want to make in my app subscription feature, so here I want to make a new post it will automatically send an email to the email who on my list subscription.

well, I'm here already have the data which it contain the subscription emails. (Table name: subs)

here my controller:

public function postAddSave() {
        $simpan= array();
        $simpan['title']=Request::input('title');
        $simpan['stock_room']=Request::input('stock_room');
        $simpan['kamar']=Request::input('kamar');
        $simpan['mandi']=Request::input('mandi');
        $simpan['sex']=Request::input('sex');
        $simpan['address']=Request::input('address');
        $simpan['description']=Request::input('description');
        $facilities = implode(',',Request::get('facilities'));
        $simpan['price']=Request::input('price');
        $simpan['coordinate']=Request::input('coordinate');

        DB::table('data_kos')->insert($simpan);
        Session::flash('success', 'Data has been created');
        return Redirect::to('home');
    }

and here my view :

<form method='post' action='' enctype="multipart/form-data">
                <input type="hidden" name="_token" value="">
                <div class="box-body">
                    <div class="box-body">
                    <div class='form-group col-sm-6'>
                        <label>Title</label>
                        <input type='text' class='form-control' name='title' value='' required/>
                    </div>
                    <div class='form-group col-sm-2'>
                        <label>Stock room</label>
                        <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-bed" aria-hidden="true"></i></span>
                    <input type="text" class="form-control" name="stock_room" required>
                    </div>
                    </div>
                    <div class='form-group col-sm-2'>
                        <label>Room Used</label>
                    <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-bed" aria-hidden="true"></i></span>
                    <input type="text" class="form-control" name="kamar" required>
                    </div>
                    </div>
                    <div class='form-group col-sm-2'>
                        <label>bathroom</label>
                    <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-child" aria-hidden="true"></i></span>
                    <input type="text" class="form-control" name="mandi" required>
                    </div>
                    </div>
                    <div class='form-group col-sm-5'>
                        <label>Address</label>
                        <input type='text' class='form-control' name='address' value='' required/>
                    </div>
                    <div class='form-group col-sm-3'>
                        <label>Price/month</label>
                    <div class="input-group">
                    <span class="input-group-addon">Rp.</span>
                    <input type="text" class="form-control" name="price" required>
                    </div>
                    </div>
                    <div class='form-group col-sm-4'>
                        <label>Category</label><br>
                        <input type="radio" name="sex" value="Wanita">Wanita</input>&nbsp;&nbsp;&nbsp;
                        <input type="radio" name="sex" value="Laki-Laki">Laki-laki</input>&nbsp;&nbsp;&nbsp;
                        <!-- <input type="radio" name="sex" value="Wanita/Laki-laki">Wanita/Laki-laki</input> -->
                    </div>
                    <div class='form-group col-sm-12'>
                        <label>Description</label>
                    <textarea class="ckeditor" placeholder="Place some text here" name="description"></textarea>
                    </div>
                    <div class='form-group col-sm-12'>
                        <label>Facilities</label>
                        <div class="checkbox">
                        @foreach ($facilities as $rows)
                        <label>
                            <input type="checkbox" value="" name="facilities[]">
                              
                        </label>
                        @endforeach  
                        </div>
                    </div>
                    <div class='form-group col-sm-12'>
                        <label>Coordinate</label>
                        <input type='text' class='form-control' name='coordinate' value='' required/>
                    </div>
                    <div class='form-group col-sm-12'>
                        <button type='submit' class='btn btn-primary'><i class='fa fa-save'></i> Simpan</button>
                    </div>
            </div><!-- /.box -->
            </form>

NB : list email to i get from table subs

so, have someone help me what improvements do i have to make to the code to achieve my goal?



via Chebli Mohamed

mercredi 22 juin 2016

Cannot upload file using guzzle and send to laravel API

I have two laravel projects, one for API and one for front end. What I want to achieve is to send the uploaded file on my front end and send it to my API using guzzlehttp6. Here's my code.

FrontEnd Laravel:

public function store(Request $request, $module_id)
{
    $videos = $request->file('file');
    // $name = time().$videos->getClientOriginalName();
    // $path = ('videos');
    // $videoFinal = $videos->move($path, $name);

    $file_name = fopen($videos, 'r');
    $client = new Client(['base_uri' => 'http://localhost/api.kourse/public/api/v1/']);

    try{
        $response = $client->request('POST', 'modules/'.$module_id.'/videos',[

            'fie_name' => $file_name

        ]);

    }catch(ClientException $e){
        return $e->getResponse();
    }


}

When returning the $videos im getting the path of the file.

Here's my API

public function store(VideoRequest $request, $moduleId)
{

    $module = Module::find($moduleId);

    if( !$module )
    {
        return response()->json([

            'message'   => 'Module does not exist',
            'code'      => 404
        ],404);
    }

    $file = $request->file('file_name');
    $name = time(). $file->getClientOriginalName();
    $path = '/videos';
    $file->move(public_path().$path, $name);
    Video::create([

        'file_name' => $name,
        'file_path' => $path.$name,
        'module_id' => $module->id
    ]);

    return response()->json([

        'message'   => 'Successfully created and assgin video to this module'
    ],200);
}

What Iam getting is that :

{message: {file_name: ["The file name field is required."]}, code: 422}
code
:
422

as defined in my VideoRequest.

Can anyone help me on this.



via Chebli Mohamed

Issue when fetching the Query String : Laravel 5.2.37

In my request class I have below code.

protected function getValidatorInstance()
{
    $instance = parent::getValidatorInstance();
    $instance->after(function ($validator) {
        $this->CheckRoleExists($validator);
    });
    return $instance;
}

public function CheckRoleExists($validator)
{
    dd($_SERVER);
}

I am trying to print the Query string and I am getting below.

"QUERY_STRING" => ""

and Url is below.

"REQUEST_URI" => "/Learning/public/UpdateRole/1"

My Route is below

Route::put('/UpdateRole/{RoleID}',array('uses' => 'Role@update', 'as' => 'UpdateRole'));

In the above Request Url: 1 is Query String. As we can see Query String value is showing null when I check it like this dd($_SERVER);

Please guide me to get the Query string correctly



via Chebli Mohamed

Send info from One browser to another in Laravel 5.2.37

I have a page where user can Add/Update records. Code is written in Laravel 5.2

Let's say I opened the that add/update page in chrome and same url in FireFox. So, if user create a new record in Chrome browser, info should be received immediately to Firefox. So, that I don't need to send ajax based reqeust to server to show complete list.

My question is, where should I start for this? Is there any blog that I can go through step by step ?



via Chebli Mohamed

Laravel 5.1 Php artisan commands not working after composer update

here is my composer.json require snippet.

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.35",
    "aws/aws-sdk-php-laravel": "~3.0",
    "lucadegasperi/oauth2-server-laravel": "5.1.*",
    "bosnadev/repositories": " 0.*",
    "laravelcollective/html": "5.1.*",
    "cartalyst/stripe-laravel": "3.0.*"
},

I ran composer update in order to add new AWS services. but then I noted that all the vendor files are updated because of composer update command. now I'm getting error when I ran php artisan commands.

This is the error message:-

PHP Catchable fatal error:  Argument 2 passed to Illuminate\Routing\UrlGenerator
::__construct() must be an instance of Illuminate\Http\Request, null given, call
ed in C:\Users\User\projects\projectxyz\projectxyzweb\vendor\laravel\framework\
src\Illuminate\Routing\RoutingServiceProvider.php on line 62 and defined in C:\U
sers\User\projects\projectxyz\projectxyzweb\vendor\laravel\framework\src\Illumi
nate\Routing\UrlGenerator.php on line 102

How can I resolve this issue? is there any wayto revert composer update without loosing my code?



via Chebli Mohamed