Click here to Skip to main content
15,394,193 members
Home / Discussions / Linux, Apache, MySQL, PHP
   

Linux, Apache, MySQL, PHP

 
GeneralRe: Help Pin
Member 1302311826-Apr-17 4:14
MemberMember 1302311826-Apr-17 4:14 
GeneralRe: Help Pin
Jochen Arndt26-Apr-17 4:43
professionalJochen Arndt26-Apr-17 4:43 
GeneralRe: Help Pin
Member 1302311826-Apr-17 4:55
MemberMember 1302311826-Apr-17 4:55 
GeneralRe: Help Pin
Jochen Arndt26-Apr-17 5:01
professionalJochen Arndt26-Apr-17 5:01 
QuestionHow To Add More Than One Entry On A Single Cell/Array ? Pin
Member 129567898-Apr-17 14:17
MemberMember 129567898-Apr-17 14:17 
AnswerRe: What is the difference between web development and Web designing? Pin
ZurdoDev21-Mar-17 1:48
professionalZurdoDev21-Mar-17 1:48 
QuestionWhere do I put my jquery and AngularJS libraries in wordpress? Pin
samflex17-Feb-17 8:14
Membersamflex17-Feb-17 8:14 
QuestionTrying to connect categories and users through pivot table i Laravel Pin
Duško Oljača3-Feb-17 1:21
MemberDuško Oljača3-Feb-17 1:21 
I have users and categories tables and pivot table categorie_users in database. In controller I make that every user can create your own categories and I make check if some category exist and if logged user have that category, he can't create new category with same name and that works fine. So next what I want is 'else' - if category exist and logged user don't have that category but he want to create it - in that case I want to connect that category which already exist and that user. Other words - I want to, let's say, category 'Music' to be just one category with that name for all future users. This is so complicated so any suggestions will appreciate. Thanks.
This is my CategoriesController.php

       public function store(Request $request)
    {
		$user = Sentinel::getUser();
		
		$category = Categories::where('name', $request->name)->get();
		
		foreach ($category as $cat){
			$user = CategorieUser::where('categorie_id',$cat->id)->where('user_id', $user->id);
		}
		
		if($category->count() > 0){
			if($user->count() > 0){
				return redirect()->route('categories.index');
			}
			else{

				/*THIS IS THAT 'else' FROM ABOVE QUESTION*/

			}
		}
		else{
			$categories = new Categories();
			$categories->name = $request->name;
			$categories->sections_id = $request->sections_id;
			$categories->save();
			$categories->users()->sync([$user->id]);
		}		
		session()->flash('success', "New category '{$categories->name}' has been created.");
		return redirect()->route('categories.index');
    }

    
And this is my model Categories.php

       <?php

        namespace App\Models;

        use Illuminate\Database\Eloquent\Model;

        class Categories extends Model
        {
	       protected $table = 'categories';

          public function sections()
          {
            return $this->belongsTo('App\Models\Sections');
          }
	
	      public function users()
          {
            return $this->belongsToMany('App\Models\Users', 'categorie_user','categorie_id','user_id');
         }
	
       }
This is model Users.php
        
        <?php

        namespace App\Models;

        use Illuminate\Database\Eloquent\Model;

        class Users extends Model
        {
	         protected $table = 'users';


	      public function categories()
		  {
			return $this->belongsToMany('App\Models\Categories', 'categorie_user','user_id','categorie_id');
		  }
	
      }

And this is pivot model CategorieUser.php (which I must create that I can go through foreach loop) 

       <?php

        namespace App\Models;

        use Illuminate\Database\Eloquent\Model;

        class CategorieUser extends Model
        {
            protected $table = 'categorie_user';
	
	
	      public function categories(){
		   return $this->belongsTo('App\Models\Categories', 'categorie_id');
	      }
	
        }

AnswerRe: Trying to connect categories and users through pivot table i Laravel Pin
Afzaal Ahmad Zeeshan3-Feb-17 2:10
mveAfzaal Ahmad Zeeshan3-Feb-17 2:10 
QuestionOutputting ROW QUERY result to main program Pin
ELMAGLAYA16-Jan-17 21:19
MemberELMAGLAYA16-Jan-17 21:19 
AnswerRe: Outputting ROW QUERY result to main program Pin
Richard MacCutchan16-Jan-17 21:53
mveRichard MacCutchan16-Jan-17 21:53 
QuestionHow to fix this error $_SESSION:Notice: Undefined offset: 0 Pin
ELMAGLAYA4-Jan-17 18:45
MemberELMAGLAYA4-Jan-17 18:45 
AnswerRe: How to fix this error $_SESSION:Notice: Undefined offset: 0 Pin
Jochen Arndt4-Jan-17 22:16
professionalJochen Arndt4-Jan-17 22:16 
GeneralRe: How to fix this error $_SESSION:Notice: Undefined offset: 0 Pin
ELMAGLAYA16-Jan-17 21:33
MemberELMAGLAYA16-Jan-17 21:33 
AnswerCentOS httpd can not access Symbolink Pin
njzt28-Dec-16 1:26
Membernjzt28-Dec-16 1:26 
QuestionOpen Suse Linux Pin
ForNow27-Dec-16 13:42
MemberForNow27-Dec-16 13:42 
AnswerRe: Open Suse Linux Pin
Richard MacCutchan27-Dec-16 21:00
mveRichard MacCutchan27-Dec-16 21:00 
GeneralRe: Open Suse Linux Pin
ForNow28-Dec-16 2:11
MemberForNow28-Dec-16 2:11 
GeneralRe: Open Suse Linux Pin
Richard MacCutchan28-Dec-16 5:45
mveRichard MacCutchan28-Dec-16 5:45 
AnswerRe: Open Suse Linux Pin
Albert Holguin9-Jan-17 7:52
professionalAlbert Holguin9-Jan-17 7:52 
QuestionWhich is the Best Laravel Admin Template with all the advanced functionalities? Pin
Rakeshroshans23-Dec-16 20:00
MemberRakeshroshans23-Dec-16 20:00 
AnswerRe: Which is the Best Laravel Admin Template with all the advanced functionalities? Pin
Richard MacCutchan23-Dec-16 21:55
mveRichard MacCutchan23-Dec-16 21:55 
Questionfail to run php script as shell Pin
njzt6-Dec-16 15:36
Membernjzt6-Dec-16 15:36 
SuggestionRe: fail to run php script as shell Pin
Richard MacCutchan6-Dec-16 22:03
mveRichard MacCutchan6-Dec-16 22:03 
QuestionDatabase connection refused. Pin
taiwokaffo6-Dec-16 4:16
Membertaiwokaffo6-Dec-16 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.