mercredi 1 juin 2016

PHP __construct with unnamed function below

I encountered this code in a larevel.

public function __construct(
    Request $request,
    UserRepository $userRepository,
    ProfileRepository $profileRepository
)

{
    parent::__construct();

    $this->request = $request;
    $this->profile_repository = $profileRepository;
    $this->model = $this->model_repository->getModel();
    $this->entity_name_plural = str_plural($this->entity_name);

}

What is the use of the unnamed function below the constructor? This is the first time I encountered this so I am not sure how it really works. Thanks for your response.



via Chebli Mohamed

Laravel Data Converter

I have a model that holds 7 digits as string. I want to create converter that the interaction between the user and that string will be like ##-###-##. So if the model holds 1234567 the user will see 12-345-67 and if the user entered the number as 76-543-21 the model stores it like 7654321.

Many other platforms are including converters to handle this kind of tasks. How can I do that in Laravel 5.2? I have searched in the docs and didn't find any helpful solution.

Is Laravel 5.2 support converters? And how should I handle such conversion?



via Chebli Mohamed

Request object type-hinted in a library method throws missing argument error

In Laravel 5.2, it's suggested to type-hint the Request object in controller method declarations:

I'm trying to do the same in one of my libraries:

<?php

namespace App\Libraries;

use Illuminate\Http\Request;

class MyLibrary {

    public static function doStuff(Request $request) {
        //...
    }
}

However, when I try to use the library method like shown in the code below, I get a missing argument error:

<?php

namespace App\Http\Controllers;

use App\Libraries\MyLibrary;

class DefaultController extends Controller {

    public function __construct() {
        MyLibrary::doStuff(); // => trows missing argument error
    }

}

Now, I've type-hinted the Request object into my doStuff() method. Why do I need to pass an argument? I thought that type-hinting is a way to inject needed resources into methods, so they don't have to be always passed directly. Do I understand the concept incorrectly?



via Chebli Mohamed

Laravel cors 'Access-Control-Allow-Origin' and 'Access-Control-Allow-Headers'

I am new to Laravel.

I tried http://ift.tt/1nXdw0q.

When I add

header('Access-Control-Allow-Origin: *');

in my public/index.php, it does not add the Content-Type in the response.

When I add

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, Content-Type');

It does not add Access-Control-Allow-Origin.

I am very confused by all various solutions I find on internet. How exactly should I go about this?



via Chebli Mohamed

Laravel - Using a nested relationship with the query builder

I am working on an API but its starting to get a bit slow now that the data is increasing. I am moving some of the queries so that they use the DB query builder.

I have my last one which has a nested query:

$artists = Artist::with('performances', 'performances.stage')->get();

I have got so far:

$artists = \DB::table('artists')
    ->leftJoin('performances', 'artists.id', '=', 'performances.artist_id')
    ->get();

But now need to do the second relationship which in the Performance model is:

public function stage()
{
    return $this->hasOne('App\Models\Stage', 'id', 'stage_id');
}

Any help on how I do this?



via Chebli Mohamed

File not found at path in laravel 5.2

I have an image within the public/badges directory called 1.png - I am trying to use the Storage library in laravel but keep getting the following error even though the file does exist at that location:

// Error

FileNotFoundException in Filesystem.php line 381:
File not found at path: Users/gk/Sites/mysite/public/badges/1.png

// Code

$filePath = 'badges/1.png';
$path = public_path()."/".$filePath
Storage::disk('local')->get($path);
dd($contents);



via Chebli Mohamed

SoftDeletes on model breaks dynamic properties

TLDR: When the SoftDeletes trait is included in my parent model, I no longer get soft deleted instances of the parent model as a dynamic property of the child. How can this be done?


I have defined a couple of basic models, like this:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Builder;

class User extends Model
{
    use SoftDeletes;

    public function posts()
    {
        return $this->hasMany("App\Post");
    }
}

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo("App\User");
    }

    public function scopePending(Builder $query)
    {
        return $query->whereNull("pending");
    }
}

In my controller, I want to list pending posts, so I do this:

<?php
namespace App\Controllers;

use App\Post;

class PostController extends Controller
{

    public function index()
    {
        $posts = Post::pending()->get();
        return view("post.index", ["pending"=>$posts]);
    }
}

And finally, in my view:

@foreach($pending as $post)
    <br/>
    <br/>
@endforeach

This results in an exception being thrown, "Trying to get property of non-object" with the line number corresponding to where I try to output $post->user->name for users which have been soft deleted.

How can I have these dynamic properties include soft deleted items?



via Chebli Mohamed