vendredi 6 janvier 2017

laravel 5.1: get relation in with

I have a model called user with relation "travel" model and travel model with relation "destination"

how can I get user in eloquent with travel and relation.

I know that I can get travel like this :

    $row = $this->model
        ->find($reqId)
        ->with('travel')
        ->first();

I want get destination of travels .



via Chebli Mohamed

Laravel factory specifying column twice

I'm using factories for unit testing and so far I haven't had any problems until now:

// This is the factory definition (note: other fields removed for brevity).
// Primary key is not included just like Laravel's docs show.
$factory->define(Media::class, function (Faker\Generator $faker) {
    return [
        'id_media' => $faker->unique()->randomNumber(6),
        'type' => $faker->randomNumber(1)
        // other irrelevant fields
    ];
});


 // This is how I call it on my unit test
 $count = 5;
 $id = 3;
 factory(Media::class, $count)->create([
      'id_media' => $id
 ]);

The problem is that this generates invalid SQL (id_media is there twice):

INSERT INTO media (id_media, type, id_media) VALUES (73052, 2)

If I don't override the id_media property when I call it in my unit test, then the query is valid but it's not what I need. Two things that strike me as odd are:

  • I'm using this all over my tests but the only time invalid SQL is generated is with this factory. I've got around two dozen factories in my app and this is the only problematic one. This particular table has one primary key and no other indexes. The table is as vanilla as they come.
  • This is actually recommended in the Laravel docs. I'm not doing anything out of the ordinary.


via Chebli Mohamed

mode rewrite issue laravel 5.1

sorry for asking a question but it drives me crazy i'm working a week on this issue but i haven't found a solution, i have a website with two languages , exp( de ,en ) i wanted to disable and enable this option in a panel and my RewriteRule applies the both conditions ,

for example if i have "localhost/test/en/admin" working this url works as well "localhost/test/admin"

so i edited my htaccess file in my root directory (note: i don't use public directory i use an index file inside of my root directory)

i have two htaccess files one in my root directory one in my public directory

and roots are as below

root:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ public/$1 [L]

</IfModule>

public:

 <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301] 



    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php         [L]

ok , when i load http://localhost/test/ everything is good , i have my stylesheets everything

but when i use http://localhost/test/en everything that contains urls broke , my domain name removes from my "url" helper function and instead when i call App::getLocale(), the return value is my domain name instead of my lang var!

//+++++++++++++++++++++++++++++++++++++++++++++++

this is my previous htacess file for my root directory

root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]

and i had this issue every url would map to index.php and i coudnt handle it with 404 error

for example "localhost/test/en" was a valid url and "localhost/test/foo" was a valid url just showing my index page of site ! and i coudnt handle a url without lang code ,

"localhost/test/en/foo" would get 404 error but "localhost/test/foo" just the index page

i'm very confused and i dont know how to handle this , thanks in advance



via Chebli Mohamed

How can I change the language of errors in laravel 5.1 dynamically

I have been using the helper

app()->setLocale(session('lang'));

However, I can get to change the languages of views but the languages of errors still continues reading of

\config\app.php locale => ‘es’

That means that always show the same language.

How can i change it also dynamically?

enter image description here



via Chebli Mohamed

jeudi 5 janvier 2017

Laravel Model not retrieving data

Im using laravel to pull some log information from a database, but all of a sudden its stopped working, as in its not retrieving data and not returning anything. Im using vuejs to retrieve the data without page refresh, theres no problem on the front end because data can still be retrieved also in the chrome debug console its displaying as a 500 error.

Furthermore what i find weird is it works locally but not in production.

Example code of what works and what doesn't

 <?php

namespace App\Http\Controllers;

use App\Log;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class LogController extends Controller
{

    public function getLogData()
    {

          //This is the original code & it doesn't work in production! 
          //$data = Log::orderBy('id', 'DESC')->select('action', 'object_id', 'ip_address', 'user', 'time', 'date')->get();

        //This works! But only retrieves 1 row of information
        $data = Log::where('id', 1)->get();

        $data = str_replace('*', "<b>", $data);
        $data = str_replace('^', "</b>", $data);

        return $data;
    }

}

and heres the logs model, which shouldnt really affect anything but entering data into the database really but just incase anyone needs this information.

namespace App;

use Illuminate\Database\Eloquent\Model;

class Log extends Model
{
    protected $fillable = ['action', 'object_id', 'object_type', 'ip_address', 'user', 'time', 'date'];
}

Any help i get will be appreciated.



via Chebli Mohamed

mardi 3 janvier 2017

Find inside comma separated values field Laravel 5.1

need to check if a variable is present inside a comma separated string in mysql field using querybuilder. I do this

                          <?php $parents = DB::table('categorie')->whereRaw('FIND_IN_SET("$categoria->id", parent)')->get();                            ?>

but didn't return any value... Any idea?



via Chebli Mohamed

lundi 2 janvier 2017

Using Laravel what is the correct way to store menu item's phrases?

I am trying to use languages with Laravel 5.3.

I have a menu named gender that shows the user two options "i.e 'Male' and 'Female'".

I want to add the phrases 'Male' and 'Female' to the language file.

What is the correct way to store menu item's translation with Laravel?

I did the following

'gender_male' => 'Male',
'gender_female' => 'Female'

But then notices that that Laravel has "what I believe it to be" nested phrases. Here is an example from the code that is shipped with the Framework

'same'                 => 'The :attribute and :other must match.',
'size'                 => [
    'numeric' => 'The :attribute must be :size.',
    'file'    => 'The :attribute must be :size kilobytes.',
    'string'  => 'The :attribute must be :size characters.',
    'array'   => 'The :attribute must contain :size items.',
],

Should my language file then look like this instead? and if so how to I access I read it?

 'gender' => [
    'male' => 'Male',
    'female' => 'Female'
 ]



via Chebli Mohamed