lundi 3 octobre 2016

Laravel 5.1 sync method with multiple attibutes as primary

I am using sync(on many to many) method in laravel 5.1 to add relational info to a user in DB.

My Table is

User-:

  • id(pk)
  • name
  • email

tags-:

  • id(pk)
  • name
  • slug

user_tags_mapping-:

  • user_id
  • tags_id
  • function
  • count

the relationship i have defined in between is a many to many

return $this->belongsToMany(
        tags::class, 'user_tags_mapping',
        'user_id', 'tags_id')->withTimestamps()
        ->withPivot('function', 'count');

Now i am using sync to insert value into this table like

$user->tags()->sync([tags_id => [function, count]]);

Now my problem is that when i use the same tags_id with different function the sync detaches the older value as the tags_is already exist.

Is There a way where i can use tags_id and function both as the parameters to sync to the table where function is a string value.

I have already defined the all the three parameter as primary inside the migration script, but it still didn't work.



via Chebli Mohamed

How do I implement Maker-Checker Concept in Laravel 5.2?

I have to implement maker-checker concept in laravel 5.2.
If User(Maker) does some changes(insert,update,delete), they will only be reflected after the Checker approves it. How can I implement it??
Thank You in Advance,



via Chebli Mohamed

dimanche 2 octobre 2016

add control to resource routes

My resource route looks like :

$router->resource('subnets', 'subnetController');

I saw on the documentation that a resource route can have an array of parameters... is there a parameter that permits you to choose who can access to those routes ? If I was using basic routing that would be something like :

Route::get('/subnets', function () {
   if (Auth::user()['attributes']['role'] == 'admin') return view('subnets.index');
   else return view ('errors.403');
});



via Chebli Mohamed

Issue while sending smtp email from Godaddy in Laravel 5.3.15

In order to send email from GoDaddy i am using below settings.

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=465
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=ssl

I am getting error message

Connection could not be established with host localhost [Connection refused #111]

This setting was working fine in Laravel 5.2 but not working in Laravel 5.3

Did you face this kind of issue before?



via Chebli Mohamed

samedi 1 octobre 2016

Seed a recursive table using factories in Laravel

I have a recursive table in my Laravel project to handle a dynamic menu. For testing purposes I need to seed that table with random elements.

The name is not the problem (a random name would be okay). The main thing is how to generate the parent_id numbers. I'll show an example:

+---------------------------+
| id  | name    | parent_id |
+---------------------------+
| 1   | animals | 0         |
|---------------------------|
| 2   | food    | 0         |
|---------------------------|
| 3   | dog     | 1         |
|---------------------------|
| 4   | potato  | 2         |
|---------------------------|
| 5   | monkey  | 1         |
+---------------------------+

I don't want to generate a random number, because some entries of the table would not have any associated parent. And since the id is incremental, the parent_id must be associated with some id.

I've created the factory for the first level (the parent_id = 0). But I don't know how to continue now.

$factory->defineAs(App\Section::class, 'firstLevelMenu', function($faker) {
   return [
       'name' => $faker->unique->randomElement([
           'animals',
           'food'
       ]),
       'parent_id' => '0'
   ];
});



via Chebli Mohamed

How to show value in if statement in laravel

I am doing my project but got stuck in this. I want to print both value of degrees and experiences. How can i achieve this? My controller file is like below:

public function industryPost (Request $request) {
        $industry = $request->input('industry');

        if (empty($industry)) {
            return 'Industry can not be null';
        } else {

            $experiences = AreaOfExperience::where('industry_id', $industry)->where('is_active', 1)->where('is_deleted', 0)->get();
            $degrees = Degree::where('industry_id', $industry)->where('is_active', 1)->where('is_deleted', 0)->get();


            if (!empty($degrees) ) {
                $string2 = '';
                foreach ($degrees as $degree) {
                    $string2 .= '
                        <div class="col-md-4">
                            <div class="be-checkbox">
                                <label class="check-box">
                                    <input id="degree" type="checkbox" name="degrees[]" value="'.$degree->id.'" class="checkbox-input">
                                    <span class="check-box-sign"></span>
                                </label>
                                <span class="large-popup-text"> '.$degree->name.' </span>
                            </div>
                        </div>
                    ';
                }
                return response($string2);
            }
            if (count($degrees) == 0) {
                return 101;
            }

            if (!empty($experiences) ) {
                $string = '';
                foreach ($experiences as $experience) {
                    $string .= '
                        <div class="col-md-4">
                            <div class="be-checkbox">
                                <label class="check-box">
                                    <input id="experience" type="checkbox" name="area_of_experiences[]" value="'.$experience->id.'" class="checkbox-input">
                                    <span class="check-box-sign"></span>
                                </label>
                                <span class="large-popup-text"> '.$experience->name.' </span>
                            </div>
                        </div>
                    ';
                }
                return response($string);
            }
            if (count($experiences) == 0) {
                return 100;
            }


        }
    }

I want to print both the value of degree and experience. But in my query it prints the value of degree. how can i achieve this? Please help!



via Chebli Mohamed

Session does not persist in laravel

laravel 5.2 session does not persist , i have not used auth::check or any auth method for login check then how to put all my routes in middleware

Route::auth();



via Chebli Mohamed