vendredi 28 avril 2017

can we detect phone number if opened in mobile device [duplicate]

This question already has an answer here:

I am thinking of a feature for that I have to search for necessary components and this is one of them.

For instance, I have PHP LARAVEL app and if I open it in mobile device can it detect phone number?



via Chebli Mohamed

Laravel: what's the better method to retrieve current logged user and why?

I know two method:

The first is using a Request object param in the controller's function

public function index(Request $request)
{   
    $user = $request->user();
    return view('home');
}

The second is using directly the Auth facade.

public function index()
{   
    $user = Auth::user(); 
    return view('home');
}

Are there any diferences? Are one method better that the other one and, if, yes, why?



via Chebli Mohamed

Form Error Messages Are Not Displayed Laravel 5

I'm new at Laravel and I want to Use the authentification system included in Laravel. To do so I activated authentification in my laravel projet with

php artisant make:auth

Then I tryed to log in in the my projet and everthing works but the error messages like "e-mail field is required" aren't shown when I submit the empty form.

Here is an example of the error test auto implemented in my login.blade.php :

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

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

Please notice that when I enter a bad combinaison of email/password the error message is shown correctly but for any other case the page doesn't even refresh.

Thank you for your help ...



via Chebli Mohamed

Eloquent relationship grouped by pivot relationship

I am trying to create a relationship for a model for an Order which will group items by their menu item, to end up with the following data structure (when outputted to JSON):

{
  "id": 123,
  "items": [
    {
      "menu_item_id": 32,
      "items": [
        {
          "id": 456,
          "order_id": 123,
          "menu_item_id": 32
        },
        {
          "id": 457,
          "order_id": 123,
          "menu_item_id": 32
        }
      ]
    },
    {
      "menu_item_id": 37,
      "items": [
        {
          "id": 466,
          "order_id": 123,
          "menu_item_id": 37
        },
        {
          "id": 467,
          "order_id": 123,
          "menu_item_id": 37
        }
      ]
    }
  ]
}

I have the following models & relationships:

class Order extends Model
{
    ...
    public function items()
    {
        return $this->hasMany('App\Models\OrderItem');
    }
    ...
}

class OrderItem extends Model
{
    ...
    public function menuItem()
    {
        return $this->belongsTo('App\Models\MenuItem');
    }
    ...
}

Any suggestions on how?



via Chebli Mohamed

MySQL Query doesn't check for condition

I have a query like this:

 select * from `research_purchases` left join `company_research_articles` on `research_purchases`.`researchable_id` = `company_research_articles`.`id` and `research_purchases`.`researchable_type` = 'Modules\Analystsweb\Entities\CompanyResearchArticle'

The research_purchases table structure is like this:research_purchases table

It is not filtering the "Modules\Analystsweb\Entities\CompanyResearchArticle" part and giving me the entire result. Any suggestions would be appreciated. Thank you.



via Chebli Mohamed

laravel fetch records from model if record exist in other model

hi i have two models named user and task. An user has many task and aleast one task to alloted to one user. i have already made one to many relationship but when i fetch and show username and no of task each user, it shows those users who has 0 task. My user class

class User extends Model
{
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'user';

/**
* The database primary key value.
*
* @var string
*/
protected $primaryKey = 'id';

/**
 * Attributes that should be mass-assignable.
 *
 * @var array
 */
protected $fillable = ['name', 'type', 'status', 'punch_time'];

public function tasks()
{
    return $this->hasMany('App\Task');
}
}

My task class

class Task extends Model
{
public $timestamps = false;
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'task';

/**
* The database primary key value.
*
* @var string
*/
protected $primaryKey = 'tsk_id';

/**
 * Attributes that should be mass-assignable.
 *
 * @var array
 */
protected $fillable = ['user_id', 'name', 'description', 'punch_time',    'status', 'redemption_time'];


public function user()
{
    return $this->belongsTo('App\User', 'id');
}
public function availableTask()
{
    return $this->task()->where('user_id','!=', 0);
}
}

my controller function $users = User::with('availableTask')->paginate($perPage); Thanks in Advance



via Chebli Mohamed

Laravel+Passport as oAuth server and axios+cordova as a client

Hello Coders,

My goal is to make an API on laravel so user get/post the data on other device /client. Client code is on axios+cordova. Am I on correct path or doing wrong. First time on Laravel + Passport API as well as on axios + cordova also.

Server Side

I setup server with the help of Laravel 5.6.4 + Passport and created token on Postman successfully.

oauth token

Client Side

Now I am trying to access user data through api/user default route in separate project, here is my code for that

AUTH_TOKEN                                     =    'Bearer eyJ0eXAiOiJKV1QiLCJ...';

axios.defaults.baseURL                         =    'http://sbs-api.dev';
axios.defaults.headers.common['Authorization'] =    AUTH_TOKEN;
axios.defaults.headers.post['Content-Type']    =    'application/x-www-form-urlencoded';

axios.get('/api/user')
    .then(function (response) {
        console.log(response);
})
.catch(function (error) {
    console.log(error);
});

but I'm unable to get user data instead of getting this error Network Error and/or Preflight Error. On Postman I am getting the data with same token.

enter image description here

Please tell me what is wrong with this code and/or provide some tutorials if possible.



via Chebli Mohamed