Friday, March 31, 2017

Laravel faker generate gender from name

What is the optimum way of generating gender using faker, having generated a name so that the gender matches the name

 return [
    'name' => $faker->name,
    'email' => $faker->safeEmail,
    'username' => $faker->userName,
    'phone' => $faker->phoneNumber,
    'gender' => $faker->randomElement(['male', 'female']),//the gender does not match the name as it is.
    'address' => $faker->address,
    'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
    'password' => bcrypt('secret')
    ];



via Morris Mukiri

Laravel searching on hasone relationship

I want to be able to perform search on values in hasOne relationship.

I have deliverables and deliverable_versions table with hasMany relationship. So a deliverable can have many deliverable_versions.

I also have deliverable hasOne deliverable_version to get the latest version.

I want to be able to list the deliverables, but after performing a search on the latest version.

The problem is, in my current database, the versions have all the information needed in search.

So for example, I want to search on the values on "recent version" like this.

$deliverables = $account->deliverables()->with('recentVersion');
$deliverables = $deliverables->search($search);
$deliverables = $deliverables->paginate(4);

How can I achieve something like this in Laravel Eloquent?

Any help is appreciated.



via Seyong Cho

Complex querie Laravel

Hi I have a complex queries

SELECT distinct libelle_adresse from adresse 
where id_adresse in (select id_adresse from zone_adresse 
where fk_zone in (select id_zone from zone 
where id_arrondissement in (select id_arrondissement from arrondissement
where libelle_arrondissement="'.$_GET['arrondis'].'")))

I've used $arrondis = $request->input('arrondis');to get the "arrondis" parameter from URL and pass it to my Controller.

This my attempt

    $term = $request->term;

    $arrondis = $request->input('arrondis');

    $query1 = DB::table('arrondissement')
    ->select('id_arrondissement')
    ->where('libelle_arrondissement','=',$arrondis)
    ->get();

    $query2 = DB::table('zone');
    ->select('id_zone')
    ->where('id_arrondissement','=',$query1)
    ->get();

    $query3 = DB::table('zone_adresse');
    ->select('id_adresse')
    ->where('fk_zone','=',$query2)
    ->get();

    $queries = DB::table('adresse');
    ->where('id_adresse','=',$query3)
    ->where('libelle_adresse','like','%'.$term.'%')
    ->take(3)->get();
    foreach ($queries as $query)
    {
        $results[] = ['id' => $query->id_adresse, 'value' => $query->libelle_adresse]; 
    }
    return response()->json($results);

I know that I get the error when I pass the "query(1/2/3) variable" in "where" but I couldn't find any solution

Any help would be appreciated. Thanks



via Marouane Sihad

Laravel Auto-Link library

I'm looking for a laravel auto-link detection.

I'm trying to make a social media website and when my users post urls i need it so a like shows insted of just normal text.



via Jack Douglas

different time in API response at first call and then call again?

I create some API in Laravel I first when I call the API in Postman it shows 504ms. Then I call back again and it shows 235ms in Postman, I call it, again it show me data near 220ms to 280ms max? If I stop calling for few minutes about 10-15 min then call it again it took around 500ms again for the first time call then I call it again it shows me near 200.

Is it cached data or persistence call or Zend opCache?

Anybody can help me how can I find it or what is it?



via Abhishek Badole

L5.3 - Call route from another route

I've a simple route into the file web.php:

Route::get('first/{param?}', [
   'uses' => 'App\Http\Controllers\MyController@index',
   'as' => 'myControllerIndex'
]);

Now, I'd like to create a second route that uses the first route but passing specific params. I tried something like this:

Route::get('second', function () {
   return file_get_contents(route('myControllerIndex', ['param' => 'book1']));
});

but it doesn't work.

Can anyone help me?

Thank you.



via vlauciani

Laravel comments and user relationship returns null

I'm trying to implement commenting system on my website but i'm having hard time with the relationships. for some reason when i try to access the user relation from the comment class it returns null.. my code: Post.php

    class Post extends Model
{
    public $timestamps = false;
    public $primaryKey = 'post_id';
    protected $fillable = ['title','created_at','username','image'];
    public function user()
    {
      return $this->belongsTo(User::class,'username');
    }
    public function votes()
    {
      //Quick note: Id refers to the post id.
      return $this->hasMany(PostVotes::class,'post_id');
    }
    public function comments()
    {
      return $this->hasMany(Comment::class,'post_id');
    }
}

User.php

class User extends Model implements Authenticatable
{
  use AuthenticableTrait;
  protected $primaryKey = 'username';
  public $incrementing = false;
  public $timestamps = false;
  protected $fillable = ['username','email','password','created_at','avatar','about'];
  // Gets avatar to display on navbar.
  public function posts()
  {
    return $this->hasMany(Post::class,'username');
  }
  public function comments()
  {
    return $this->hasMany(Comment::class,'username');  
  }
  public static function getAvatar()
  {
     return self::Where('username', '=', Session::get('username'))->value('avatar');
  }

}

Comment.php

    class Comment extends Model
{
    public $timestamps = false;
    public $primaryKey = 'post_id';
    protected $fillable = ['comment','created_at','post_id','username'];
    public function user()
    {
        $this->belongsTo(User::class,'username') ;
    }
    public function post()
    {
        $this->belongsTo(Post::class,'post_id');
    }
}
  @foreach($post->comments as $comment)
 // null
 
@endforeach



via flex_

Advertisement