dimanche 28 août 2016

EAV is 'bad', so I need hundreds of tables?

I'm building a product catalog with hundreds of different product types. These product types have a lot of different attributes. Some are shared, but most are specific for the product type.

My initial thought was to go with an EAV-type structure, but I understand why that might be a bad choice. Especially since I want my database to enforce correctness and consistency, which is going to be a mess with EAV.

The alternative in my case would be class table inheritance. I'm a bit worried about the maintainability though... how am I going to maintain hundreds of migrations and models? Is that really a desirable situation? I understand the benefits, but isn't maintainability a huge downside?



via Chebli Mohamed

Call to undefined method Illuminate\Auth\TokenGuard::login()

I try to register a new user to my app using only JSON and api guard with token driver. I got this strange error :

FatalThrowableError in RegistersUsers.php line 32:
Call to undefined method Illuminate\Auth\TokenGuard::login()
in RegistersUsers.php line 32
at RegisterController->register(object(Request))
at call_user_func_array(array(object(RegisterController), 'register'), array(object(Request))) in Controller.php line 55
at Controller->callAction('register', array(object(Request))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(RegisterController), 'register') in Route.php line 189
at Route->runController() in Route.php line 144
at Route->run(object(Request)) in Router.php line 642
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in RedirectIfAuthenticated.php line 24
at RedirectIfAuthenticated->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Router.php line 644
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 618
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53



via Chebli Mohamed

Eloquent ORM returns null using with() eager load

When using Eloquent's with() the eager loaded data sometimes becomes null. In my case, I have three tables, Users, Blogposts and Categories. The blogposts table has two foreign keys, called author and category. The query performed is given by

return Blogpost::with(['author', 'category'])->get();

The response is, well, odd. Note that author becomes null from id: 3 and category from id: 4.

In reality both the ids 1, 3 have the same author (superuser) as do the ids 2, 4 (steve). Also id 1, 4 have the same category (web dev).

It seems as if a/an category/author is already retrived and bounded to a blogpost the past categories/authors becomes null.

[
  {
    id: 4,
    author: null,
    image_name: "banner1.png",
    category: null,
    intro: "Hello World!",
  },

  {
    id: 3,
    author: null,
    image_name: "banner1.png",
    category: {
        id: 3,
        name: "big data",
    },
    intro: "Hello World!",
  },

  {
    id: 2,
    author: {
        id: 2,
        email: "foo@foo.foo",
        fullname: "foo foo",
        is_admin: false,
    },
    image_name: "banner1.png",
    category: {
        id: 2,
        name: "science",
    },
    intro: "Hello World!",
  },

  {
    id: 1,
    author: {
        id: 1,
        email: "superuser@superuser.com",
        fullname: "superuser superuser",
        is_admin: true,
    },
    image_name: "banner1.png",
    category: {
        id: 1,
        name: "web dev",
    },
    intro: "Hello World!",
  }
]

Finally here are the relations defined in the models

// User model
public function blogposts() {
    return $this->hasMany('App\Blogpost', 'id');
}

// Blogpost model
public function author() {
  return $this->belongsTo('App\User', 'id');
}

public function category() {
  return $this->belongsTo('App\Category', 'id');
}

// Category model
public function blogposts() {
  return $this->hasMany('App\Blogpost', 'id');
}

Is there any explanation to this?



via Chebli Mohamed

How can I get sessions created earlier in laravel

I need to sort my sessions by created earlier in Laravel, And I don't use session table. To do this you have any suggestions? I don't know by filename or any ? thanks all and sorry for my terrible English grammer.



via Chebli Mohamed

Convert Mysql Query to Laravel 5 Eloquent

I am using Laravel 5.1 in my application. My MySQL Query is

$result = DB::select(DB::raw("Select * from "
            . "(Select u.* from "
            . "( "
            . "SELECT u2.id as user_id, u2.fname as fname, u2.lname as lname, u2.uname as uname, u2.email as email, u2.address as address, u2.city_id as city_id, u2.website as website FROM users u1, users u2 where u1.city_id = u2.city_id && u1.id = '$this->current_user_id' ) 
                u left join follows f on u.user_id = f.following_id where f.following_id is null 
                UNION
                Select uu.id as user_id, uu.fname as fname, uu.lname as lname, uu.uname as uname, uu.email as email, uu.address as address, uu.city_id as city_id, uu.website as website from users uu where uu.id in (Select u.user_id from (SELECT user_id FROM reviews group by user_id order by count(user_id) desc5
                ) u left join follows f on u.user_id=f.following_id where f.following_id is null
                )
                ) 
                T LIMIT 5"
    ));

How Can I convert it in Laravel Eloquent?



via Chebli Mohamed

Laravel 5.2 - Sweet Alert confirmation box

I have Categories listed in a view. A delete category button is also there in the view which does work and deletes the category when clicked.

What I want to do is before deleting a category, a sweet alert dialog to pop up and ask for confirmation. If confirmed, it should go to the defined route and delete the category.

The delete link is defined like this:

<a id="delete-btn" href="" class="btn btn-danger">Delete</a>

and the script is defined like this:

<script> 
    $(document).on('click', '#delete-btn', function(e) {
        e.preventDefault();
        var link = $(this);
        swal({
            title: "Confirm Delete",
            text: "Are you sure to delete this category?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, delete it!",
            closeOnConfirm: true
         },
         function(isConfirm){
             if(isConfirm){
                window.location = link.attr('href');
             }
             else{
                swal("cancelled","Category deletion Cancelled", "error");
             }
         });
    });
</script>

However, when I click the delete button it deletes the category, but the sweet alert message doesn't show up.

The route is defined as following:

Route::get('/categories/destroy/{category}', [
    'uses' => 'CategoriesController@destroy',
    'as' => 'admin.categories.destroy',
]);

and the controller function is defined as:

public function destroy(Category $category) 
{

    $category->delete();

    //this alert is working fine. however, the confirmation alert should appear 
    //before this one, which doesn't
    Alert::success('Category deleted successfully', 'Success')->persistent("Close");

    return redirect()->back();
}

Any help would be appreciated. Thanks.



via Chebli Mohamed

samedi 27 août 2016

How injecting a array in my view with laravel 5 using a facade and the App:make method in the view

I created a facade that works well, a using service container :

My class :

namespace App\Classes;

class Administration {

  public function message_success()
{
   $message = "success";

  return $message;

  }

}

My view :

Résultat Ok

But when I want to work with a array, I can not retrieve a value in my view with App::make.

My class :

namespace App\Classes;

class Administration {

public function message_test($message, array $type = array())
{

  $type = ['message1' => 'valeur1', 'message2' => 'valeur2'];

  return $message;

  }

}

How to build the syntax in my view?



via Chebli Mohamed