lundi 27 juin 2016

Split results into 5 columns

I have a query like:

$results = Post::all();

For the sake of simplicity, let's say the output is this (by id):

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

My website has a 5-column layout. I need to split/divide these results into 5 columns such that it ends up like this:

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

The 5 column layout is split like this (the total number of columns is 25):

<div class="row">
    <div class="col-xs-5">
        //
    </div> 
    <div class="col-xs-5">
        //
    </div> 
    <div class="col-xs-5">
        //
    </div> 
    <div class="col-xs-5">
        //
    </div> 
    <div class="col-xs-5">
        //
    </div> 
</div>

How can I split $results up like this? Any way to make use of Laravel's Collection methods?



via Chebli Mohamed

Language implementation in Database: Laravel 5.2

What Am I doing?

I was using lang folder to maintain the translation for two languages. Now, as per the requirement, admin can also do the Language Key values changes. So, now I have moved it to database.

ok, Now my array is like below.

Collection {#191 ▼
  #items: array:6 [▼
    0 => TranslationModel {#192 ▼
      +table: "tbltranslation"
      +primaryKey: "TranslationID"
      +timestamps: true
      #connection: null
      #perPage: 15
      +incrementing: true
      #attributes: array:7 [▼
        "TranslationID" => 1
        "PageID" => 1
        "LanguageID" => 1
        "KeyID" => 1
        "Val" => "All Roles"
        "created_at" => "2016-06-27 21:37:06"
        "updated_at" => "2016-06-27 21:37:06"
      ]
      #original: array:7 [▶]
      #relations: []
      #guarded: array:1 [▶]
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => TranslationModel {#193 ▶}
    2 => TranslationModel {#194 ▶}
    3 => TranslationModel {#195 ▶}
    4 => TranslationModel {#196 ▶}
    5 => TranslationModel {#197 ▶}
  ]
}

There are 6 places in the blade where I have to show Translation from above array. I am thinking to write below code at each place inside blade.

Below is for first place.

$Translation_Dictionary->where('KeyID', 1)->first()

Am I doing it correctly?



via Chebli Mohamed

Custom function on model

I have a query like this:

$results = User::where('gender', 'male')
    ->get();

Is there a way that I can call a custom function on this query, like:

$results = User::where('gender', 'male')
    ->get()
    ->split();

Where split() would be the following function:

public function split() {
    return array_chunk($results->toArray(), 10);
}

How would I implement something like this? Where would the function go?



via Chebli Mohamed

How to pass sendgrid email template ID to mail send function?

I saw lot of thread is there to explain send email with template using sendgrid. But I have email template in third party sendgrid server not in my project folder. Only template ID is with me. I am using this package and my email send function is like that as explain in the readme.

\Mail::send('view', $data, function (Message $message) {
$message
    ->to('foo@example.com', 'foo_name')
    ->from('bar@example.com', 'bar_name')
    ->embedData([
        'category' => 'user_group1',
        'unique_args' => [
            'user_id' => 123
        ]
    ], 'sendgrid/x-smtpapi');
});

when I pass a view in my views folder variable 'view' it works well. But my question is how to pass template ID in the sendgrid and use that template from my code ? Please help.



via Chebli Mohamed

Laravel button does not link to route

@extends('layout')

@section('content')
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h1></h1>

            <ul class="list-group">
                @foreach($id->notes as $note)
                    <li class="list-group-item">
                        
                        <span class="pull-right">
                                <button href="/notes//edit" type="button" class="label label-default pull-xs-right">Edit</button>
                        </span>
                    </li>
                @endforeach
            </ul>

            <hr>
            <h3>Add a New Note</h3>

            <div class="form-group">
                <form method="post" action="/cards//notes">
                    <div class="form-group">
                        <textarea name="body" class="form-control"></textarea>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">Add Note</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@stop

The button in the foreach loop has the correct link, /note/id/edit. I can type that into chrome and go to the edit page. However, the button does not head there. I just see the click animation. Why would this be?



via Chebli Mohamed

Undefined Offset: 1 in Laravel 5 Due to Middleware

I'm Getting this error with the route add-site.

ErrorException in compiled.php line 7315: Undefined Offset: 1

Here are the files...

routes.php

Route::get('add-site',['middleware' => 'auth',function()
{
    return view('dashboard.add_site');
}]);

Middleware

public function handle($request, Closure $next)
{
    if(Session::has('sa_loggedin')==FALSE and !Session::has('sa_server_id'))
    {
        return "error!";
    }
    else
    {
        return $next($request);
    }

    return 0;
}

I know the error might be silly but I'm not getting it... I'm trying to solve this error since hours...



via Chebli Mohamed

Restricting routes with auth middleware group in laravel 5.2

I'm trying to prevent people from accessing the /dashboard route unless they're authenticated(logged in). I looked at the laravel docs and here's what I thought I was supposed to do to accomplish this. Although I'm pretty sure I don't need the 'middleware' => 'auth' line in the dashboard route.

Route::group(['middleware' => 'auth'], function (){
    Route::get('/dashboard', [
        'uses' => 'UserController@getDashboard',
        'as' => 'dashboard',
        'middleware' => 'auth'
    ]);
});



via Chebli Mohamed