dimanche 16 septembre 2018

Access user via Authorizer in model definition

In Laravel 5.1 with OAuth2, I want to eager load a model, but place query params (from user model) on the eager loaded model. The easiest way I thought to do this was directly on the model.

More specifically, Activity belongsTo Task. I only want to load activities for the task where they match certain criteria on the user object. Using OAuth2, I attempt to find the user via Authorizer, then access its properties. The route is wrapped in Lucadegasperi's middleware

$router->group(['middleware' => 'oauth'], function(Router $router) {
       $router->get('tasks/{task}', 'Resources\Tasks@show');

And the request header seems to pass along the bearer token just fine.

I'm able to access the user via Authorizer within the controller, but not in the model definition. Why is this?

I'm getting error:

NoActiveAccessTokenException in Authorizer.php line 104: Tried to access session data without an active access token

use LucaDegasperi\OAuth2Server\Facades\Authorizer;
use App\User;

class Task extends BaseModel
{
    protected $with = ['activities'];

    public function activities()
    {
        $user = User::find(Authorizer::getResourceOwnerId());
        return $this->hasMany(Activity::class)->where('id', '=', $user->...);
    }



via Chebli Mohamed

samedi 15 septembre 2018

how to make db in laravel using mysql for multiple users, like in my case Admin, Users, Agents, International Agents

i want to design a db for my project in laravel but confused how to manage tables. I have four entities Admin, Users, Agents and International Agents. Please help me how i manage my db tables. and if i make single table for multiple users then how to identify users. Note: Each of the mentioned entities are different. Please ask if it not sounds clear. Thanks



via Chebli Mohamed

Laravel Problem credentials do not match our records

First of all Sorry for my bad english.

I am new to laravel. I have recently run into some problems with authentication/login system which comes built-in in laravel by the command make:auth.

The registration process is working correctly and after that it logs me in automatically. but when I log out and try to log back in, I get this error:

These credentials do not match our records.

I'm not sure whats wrong! but i tried to search error on google stack overflow and etc. The solution i found is this. These credentials do not match our records, laravel 5.2 but it also didn't work.

This is my code.

Http/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;


class LoginController extends Controller
{

    use AuthenticatesUsers;


    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }


}

This is model

User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'user_name', 'email','user_password','user_created_by',
    ];

    protected $hidden = [
        'user_password', 'remember_token',
    ];

    protected $primaryKey = 'user_id';

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

This is View

Views/auth/login.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Login</div>

                <div class="panel-body">
                    <form class="form-horizontal" method="POST" action="">
                        

                        <div class="form-group">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="" required autofocus>

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong></strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="password" class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong></strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember" > Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-8 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Login
                                </button>

                                <a class="btn btn-link" href="">
                                    Forgot Your Password?
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

This is User Table

User Table



via Chebli Mohamed

vendredi 14 septembre 2018

Hide row when result of spesific column is null in Datatable Jquery automatically?

i want to ask about how to hide one row, in example one row contain 10 columns, the first and second column is filled with right value, but third until end value is null, i want to hide the row that have the condition like that. i've tried to make filter before its rendered but not work, please help. This is my code

oTable = $('#data').DataTable({
   "ordering":true,
   "columns": [
   {data: 'no', name: 'no'},
   {data: 'number', name: 'number'},
   {data: 'action', name: 'action'},
   {data: 'status', name: 'status'},],});
   //Used For Call Data (AJAX)

<table id="data" class="table table-striped table-bordered display" style="width: 100%; overflow-x: scroll" >
  <thead>
  <tr>
    <th style="width: 25px">No</th>
    <th>No</th>
    <th>Action<br></th>
    <th>Current Status</th>
  </tr>
 //Used to make table

Thank you. Any idea and help will much appreciated



via Chebli Mohamed

jeudi 13 septembre 2018

How to sotre an id from a collection on to another collection of MongoDB in Laravel

I have two Collection category and subcategory. One category can have 0 or more subcategories that's why i need relation between them how can i store _id oa document form category collection to subcategory collection's document.

category

{
    "_id" : ObjectId("5b8abe366a44fa2d080065d2"),
    "Title" : "Aptitude",
    "Description" : "Aptitude",
    "Slug" : "aptitude",
    "updated_at" : ISODate("2018-09-01T16:28:38Z"),
    "created_at" : ISODate("2018-09-01T16:28:38Z")
 }

subcategory

{
    "_id" : ObjectId("5b9abbd06a44fa207c001674"),
    "Title" : "Aithmatic Apptitude",
    "Description" : "Aithmatic Apptitude",
    "Slug" : "arithmatic_aptitude",
    "CategoryId" : "5b8abe366a44fa2d080065d2",
    "updated_at" : ISODate("2018-09-13T19:34:40Z"),
    "created_at" : ISODate("2018-09-13T19:34:40Z")
}
{
    "_id" : ObjectId("5b9ac7e86a44fa207c001678"),
    "Title" : "Reasoning",
    "Description" : "Reasoning",
    "Slug" : "reasoning",
    "CategoryId" : "5b8abe366a44fa2d080065d2",
    "updated_at" : ISODate("2018-09-13T20:26:16Z"),
    "created_at" : ISODate("2018-09-13T20:26:16Z")
}

As you can see i am storing the category_id on subcategory collection a string "CategoryId" : "5b8abe366a44fa2d080065d2",

Here is my code for storing

$subcategory = Subcategory::create(
    [
        'Title' => $request->Title, 
        'Description' => $request->Description,
        'Slug' => $request->Slug,
        'CategoryId' =>  $request->CategoryId,
    ]

);

Want to sore it like actual reference"category_id" ObjectId("5b8abe366a44fa2d080065d2") . convert this command db.subcategory.insert({Title:"Example",Description:"Example", Slug:"Example",CategoryId:db.category.find({_id:"5b9abc8cb3562ec4e990ec5b"})._id}) how can it be possible, Please help



via Chebli Mohamed

Whan to Show some data On my view but there is a Error In Laravel

Here I am trying to add, Add Comment feature and it's causing a problem to show data.

public function store(Request $request)
{
    $chatterreply =  new Chatterreply;
    $chatterreply->reply = $request->body;
    $chatterreply->chatter_post_id = $request->chatter_post_id;
    $chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
    $chatterreply->save();
    $chatterreplies = Chatterreply::where('chatter_post_id',$request->chatter_post_id)->get();
     $chatter_editor = config('chatter.editor');

    if ($chatter_editor == 'simplemde') {
        // Dynamically register markdown service provider
        \App::register('GrahamCampbell\Markdown\MarkdownServiceProvider');
    }
    echo "<pre>"; print_r($chatterreplies); die;
    return view('chatter::discussion', compact('chatterreplies','chatter_editor'))->with('chatter_alert','Add Comment Successfully');

}

And Here Is Where I am passing the variable

 $chatter = session()->pull('chatter');
            return view('chatter::discussion', compact('discussion', 'posts', 'chatter_editor'))->with('chatterreplies',$chatter);



via Chebli Mohamed

How to prevent the main Laravel form to submit?

I have 1 main form. I also have a sub form inside that main form.

I can't seem to prevent the main form to stop submitting since the button type submit will submit the main form automatically.

That is my entire form

enter image description here

My subform is my delete of each image

enter image description here

edit.blade.php

@extends('layouts.be.master')
@section('content')

<script type="text/javascript" src="/js/ckeditor.js"></script>
<link rel="stylesheet" type="text/css" href="http://d.biossusa.com/css/root/hover-master/hover.css">

<style type="text/css">
.portfolio-images {
    border: solid 1px silver;
}

._hover{
    padding: 0px;
    position: relative;
    overflow: hidden;
    border: 1px solid #D8D8D8;
    /*border-radius: 10px;*/
}
._hover:hover .caption{
    opacity: 1;
    transform: translateY(-150px);
    -webkit-transform:translateY(-150px);
    -moz-transform:translateY(-150px);
    -ms-transform:translateY(-150px);
    -o-transform:translateY(-150px);
}
._hover img{
    z-index: 4;
}
._hover .caption{
    position: absolute;
    top:150px;
    -webkit-transition:all 0.3s ease-in-out;
    -moz-transition:all 0.3s ease-in-out;
    -o-transition:all 0.3s ease-in-out;
    -ms-transition:all 0.3s ease-in-out;
    transition:all 0.3s ease-in-out;
    width: 100%;
}
._hover .blur{
    background-color: rgba(0,0,0,0.8);
    height: 300px;
    z-index: 5;
    position: absolute;
    width: 100%;
}
._hover .caption-text{
    z-index: 10;
    color: #fff;
    position: absolute;
    height: 300px;
    text-align: center;
    top:-20px;
    width: 100%;

}
</style>


<?php $tags = explode(",", $portfolio->tag ); ?>

<div class="card-body card-padding">
    <div class="row">

        {!! Form::open(array('class' => 'form-horizontal', 'role' =>'form', 'url'=>'portfolio/'. $portfolio->id .'/update','files' => true)) !!}


        <div class="col-sm-12">

            
            <div class="form-group">
                <label for="email" class="col-sm-2 control-label">Name</label>
                <div class="col-sm-10">
                    <input type="text" value="" name="name" class="form-control" id="name" placeholder="Name">
                </div>
            </div>

            
            <div class="form-group">
                <label for="email" class="col-sm-2 control-label">Type</label>
                <div class="col-sm-10">
                    <select name="type" class="form-control">
                      @foreach($portfolioTypes as $item)
                      <option value=""></option>
                      @endforeach
                  </select>
              </div>
          </div>


          
          <div class="form-group">
            <label for="email" class="col-sm-2 control-label">Tags</label>
            <div class="col-sm-10">

            </div>

            <input id="tags" name="tag">
        </div>

        
        <div class="form-group">
            <label for="url" class="col-sm-2 control-label">URL</label>
            <div class="col-sm-10">
                <input name="url" type="text" value="" class="form-control" placeholder="URL">
            </div>
        </div>


        


        <div class="form-group">

            <label for="email" class="col-sm-2 control-label">Images</label>


            @foreach($images as $image)

                <?php

                $crop_img = str_replace('full.jpg','crop.jpg',$image->img_path);

                ?>


                <div class="col-sm-2">

                    <div class="_hover " style="background: transparent;">
                        <p style="text-align:center;">
                            <img class="img-responsive" src="" alt="">
                        </p>
                        <div class="caption">
                            <div class="blur"></div>
                            <div class="caption-text">
                                <h6 class="title lighter" style="padding:5px;border-radius: 10px;">
                                    
                                </h6>
                                <p>
                                    <span>
                                        <a data-toggle="modal"  data-target="#delete_image_">
                                            delete
                                        </a>
                                    </span>

                                </p>

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


                <div class="modal fade" id="delete_image_">
                    <div class="model-content" style="margin-top: 200px;">
                        <div class="col-sm-offset-4 col-sm-4 ">

                        {!! Form::model($image, array( 'url' => '/portfolio/image/'.$image->id.'/destroy','method' => 'DELETE')) !!}

                            <button type="submit" class="btn btn-danger btn-lg btn-block">Delete ()</button>
                          <a class="btn btn-primary btn-lg btn-block" data-dismiss="modal" > Cancel </a>

                        {!! Form::hidden('$id', $image->id)!!}
                        {!! Form::close()!!}
                        </div>
                    </div>
                </div>



            @endforeach

            <br><br>

            <input id="images" name="images[]" type="file"  multiple>

        </div>

        
        <div class="form-group">

            <label for="email" class="col-sm-2 control-label">Description</label>
            <div class="col-sm-10">

                <textarea name="description" rows="20" cols="80" id="description">
                    
                </textarea>
                <script>
                    CKEDITOR.replace( 'description' );
                </script>

            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-4 col-sm-8">
                <a class="btn btn-default" href="/portfolio"> Cancel </a>
                <button type="submit" class="btn btn-info">Done</button>
            </div>
        </div>

        </div>

        {!!Form::close()!!}


    </div>
</div>


@stop

@section('custom-scripts')

<link rel="stylesheet" type="text/css" href="/css/jquery.tag-editor.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.2/jquery-ui.min.js"></script>

<script type="text/javascript" src="/js/jquery.caret.min.js"></script>
<script type="text/javascript" src="/js/jquery.tag-editor.js"></script>

<script type="text/javascript">
    $( "select[name*='type']" ).val("");

    function readLogo(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#portfolio-icon').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

    $( "input[name*='logo_path']" ).change(function(){
        readLogo(this);
    });


    $('#tags').tagEditor({

        autocomplete: {
            delay: 0,
            position: { collision: 'flip' },
            source: [<?php echo '"'.implode('","', $skillList).'"' ?>]
        },
        forceLowercase: false,
        delimiter: ',', /* space and comma */
        placeholder: 'Enter tags ...',
        initialTags: [<?php echo '"'.implode('","', $tags).'"' ?>]

    });

</script>
@stop



Questions

How would one go about and debug this further ?


I'm open to any suggestions at this moment.

Any hints/suggestions / helps on this be will be much appreciated!



via Chebli Mohamed