lundi 1 août 2016

Broken Email Template

I am using laravel 5 framework for a project and it has a features that a user can construct a email message. But when I test it on my email I got html tags.

<p> <small>Julius Cesar</small></p><p><small>Julius</small></p><p><small> Cesar<br></small></p><p><small>tester@mail.com </small> <br></p> 

Expected Output that the user receives from email should be:

Julius Cesar
Julius
Cesar
tester@mail.com

I have no idea why it goes like that. Here is my code in laravel. This is for the view of that displays the form: eblast-form.blade.php

<form action="  " id="eblast-form" method="post">
    
    <div class="box-body">
        <div class="form-group ">
            <label for="subject" class="control-label col-sm-2">Subject</label>
            <div class="col-sm-10">
                <input type="text" name="subject" class="form-control" id="subject" placeholder="Subject" value="" required="required" />
            </div>
        </div>
        <br><br>
        <div class="form-group ">
            <label for="message" class="control-label">Email Message Content</label>
            <div class="col-sm-12">
                <textarea class="form-control" name="message-cont" id="message" required>
                    
                </textarea>
                <p><small> [fullname] [first_name] [last_name] [email]  </small></p>
                <input type="hidden" id="msg" name="message" value="">
            </div>
        </div>
    </div>
    <br>

    <br>
    <div class="box-footer">
        <div class=" pull-right">
         <button type="submit" class="btn btn-info " id="send-mail">Send</button>

        </div>
    </div>
</form>

This is on my controller's method that sends the email:

 public function postEmailblast(Request $request)
    {        
        ....
        $user = Auth::user();
        $message = $request->input('message');
        $m_user = User::find($egm->user_id);
        $rep_this = array(
            "[fullname]", 
            "[first_name]", 
            "[last_name]", 
            "[email]"
        );
        $rep_with = array(
            $m_user->common_name, 
            $m_user->first_name, 
            $m_user->last_name, 
            $m_user->email
        );
        $data = array(
            'email_content' => str_replace($rep_this, $rep_with, $message),
        );

        Mail::send('emails.e-blast', $data, function ($message) use ($request, $user, $m_user) {

            $message->from($user->email, $user->common_name)->to($m_user->email, $m_user->common_name)->subject($request->input('subject'));

        });
        ....
    }

This is the email template of emails.e-blast / emails.e-blast.blade.php

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
</head>
<body>

</body>
</html>

Does anyone have an idea about my case?



via Chebli Mohamed

How to chunk results into 3 columns

I have a set of results (e.g. [1,2,3,4,5,6,7,8,9,10,11]).

Which I want to be displayed as 1 row with 3 columns

1|5|9
2|6|10
3|7|11
4|8|

What I get is 1 row with 4 columns

1|4|7
2|5|8
3|6|9

10|
11|

Until I add a 12th object then I get 1 row with 3 columns

1|5|9
2|6|10
3|7|11
4|8|12

My code in blade template

<!-- partials/tables/table_list.blade.php -->

@section('tables')
<?php $chunkSize = floor(count($tables) / 3); ?>
<section id="tables-overview">
    <div class="row">
        @foreach ($tables->chunk($chunkSize , 1) as $chunk)
        <div class="col-md-4">
            @include('partials.tables.table_chunk')
        </div>
        @endforeach
    </div>
</section>
@endsection

<!-- partials/tables/table_chunk.blade.php -->

<table class="table table-responsive">
<thead>
    <tr>
        <th class="text-center">@lang('table.identifier')</th>
        <th class="text-center">@lang('table.property')</th>
        <th class="text-center">
            @permission('manage-tables')
                <a href="">@lang('action.create')</a>
            @endpermission
        </th>
    </tr>
</thead>
<tbody>
    @foreach ($chunk as $table)
    <tr>
        <td class="text-center"></td>
        <td class="text-center"></td>
        <td class="text-center">
            @permission('manage-tables')
                <a class="btn btn-primary" href="">@lang('action.update')</a>
            @endpermission
        </td>
    </tr>
    @endforeach

</tbody>
</table>

When the number of $tables can be divided by 3 (number of columns I want) I get 3 chunks. If not I get 3 chunks + the remaining 1 or 2 objects which both get put in a 4th column. I can place them horizontally as done here but I find this to be 'odd'. When reading a list you read from top to bottom first and then left to right.



via Chebli Mohamed

Auth::once($credentials) always false

I have one route, this route call my controller. Inside this controller I have :

  $user = Auth::loginUsingId(1); // This data get from a form
  $mail = $user->email;
  $pass = $user->password;     

  $credentials = [
   'email'    => $mail,
   'password' => $pass,
  ];

  if (Auth::once($credentials)) {
      return Auth::user()->id;
  }

  return \App\Login::where('email', '=', $mail)->where('password', '=',$pass)->first(); // I can see my user, with his email and password. It is a exmplante, later when this is working I will put false

My model is Login.php instead of User.php

class Login extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract {

use Authenticatable, Authorizable, CanResetPassword;

protected $table = 'logins';
protected $fillable = ['email','password'];
protected $hidden = ['created_at','updated_at_at'];

public function setPasswordAttribute($password)
{
    $this->attributes['password'] = bcrypt($password);
}

And I change the file config/auth.php

'driver' => 'eloquent',
'model' => \App\Login::class,
'table' => 'logins',

'password' => [
    'email'  => 'emails.password',
    'table'  => 'password_resets',
    'expire' => 60,
],

I try use the Auth::attempt() but I have the same problem, I do not understand because I can connect my database, I config the new model in config/auth ...



via Chebli Mohamed

Calling function sequentially in vuejs

I am having some problem in vuejs in executing a function/method sequentially. I have three functions like:

MethodA: function(){
    if(x = 1){
        value1 = 2;
    }

    if (x ==2){
         value2 = 4;
    }

    this.MethodB();
}

MethodB: function(){
    Total value = value1 + value2;
}

MethodC: function (){
    this.$http.get('api/getvalue').then(function(response){
        this.set('somedata', response.data);

        response.data.forEach(para){
            if(para.id == 1){
                this.MethodA();
            }
            if(para.id == 2){
                this.MethodB();
            }
        }
    });
}

ready: function(){
    this.MethodC();
}

I would like to execute this.MethodB() only after MethodA and MethodB has completely executed. How can I do this?



via Chebli Mohamed

Undefined variable in view when pulling specific data

It says undefined variable when I try to pull variable through compact

this is my controller

public function show()
{
    $list = List::find(1)->task();
    return view('configuration.configuration', compact($list));
}

this is my view

@foreach($list as $value)

           <span> </span>

@endforeach



via Chebli Mohamed

PhpUnit testing, how to check a checkbox if there are multiple checkboxes with same name in a form

I am testing a form. In the form, there are some checkboxes which are with the same name as there are multiple checkboxes to select from.

So my check boxes are like this:

<div class="col-sm-10">
    <div class="checkbox">
        <input id="department_1" name="departments[]" type="checkbox" value="1">
        <label for="department_1">Sales</label>
    </div>
                                            <div class="checkbox">
        <input id="department_2" name="departments[]" type="checkbox" value="2">
        <label for="department_2">Marketing</label>
    </div>
                                            <div class="checkbox">
        <input id="department_3" name="departments[]" type="checkbox" value="3">
        <label for="department_3">Tech Help</label>
    </div>
</div>

My testing code is like this:

public function testUserCreation()
    {
        $this->be(User::find(10));

        $this->visit('/users/create')
            ->type('First', 'first_name')
            ->type('Last', 'last_name')
            ->type('test@esample.com', 'email')
            ->type('123456', 'password')
            ->type('123456', 'password_confirmation')
            ->check('departments')
            ->press('Submit')
            ->seePageIs('/users');
    }

When I am trying to check if throws error:

InvalidArgumentException: Nothing matched the filter [permissions] CSS query provided for



via Chebli Mohamed

Laravel default guard api - use web guard for login route

how to change the guard for a special route?

i've configured the api guard as default

  'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],

now i've a route /login to authenticate the user with basic.once and send back the api token

Route::get('/login', ['middleware' => 'auth.basic.once', 'uses' => 'ApiController@login']);

with the following error

ErrorException in AuthManager.php line 288:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\TokenGuard' does not have a method 'onceBasic'

as he try to use the default api guard...

i want to keep api as default guard, just want to use web/auth.basic.once for the single /login route

thanks



via Chebli Mohamed