Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

My solution is a bit ugly. Try improving it a bit.

$response = [];
$i=0;
$videos = Video::join("videos_languages","video_id","=","videos.id")->whereRaw("approved = 0")->get()->orderBy('videos.id','ASC');


foreach($videos as $video){
	if($i!=0){
		if($response[$i]['id']==$video->id){
			$response[$i]['languages'][] = $video->language_name;
		}else{
			$response[] = setVideo($video);
			$i++;
		}
	}else{
		$response[] = setVideo($video);
	}
    
}

function setVideo($video){
	return [
		'id' => $video->id,
		'approved' => $video->approved,
		'languages' => [
			$video->language_name
		]
	];
}
Last updated 2 years ago.
0

The best way to do this would be to set these up as Elequent models and then create relationships on the models, then use those. You can drop the raw SQL. I did the same thing when I started using Laravel but there is a better way.

Hopefully this helps, if you have questions come back and ask, I will try to help.

<?php

class Video extends Eloquent{
	//DATABASE TABLE
	protected $table = 'videos';

	//FORM VALIDATION RULES
	public static $rules = array(
		'name'=>'required'
	);
	public static function validate($data) {
		return Validator::make($data, static::$rules);
	}

        //RELATIONSHIP TO LANGUAGE
	public function languages() {
		return $this->belongsToMany('Languages', 'videos_languages', video_id', 'language_id');
	}
}
<?php

class Languages extends Eloquent{
	//DATABASE TABLE
	protected $table = 'languages';

	//FORM VALIDATION RULES
	public static $rules = array(
		'name'=>'required'
	);
	public static function validate($data) {
		return Validator::make($data, static::$rules);
	}

        //RELATIONSHIP TO VIDEO
	public function languages() {
		return $this->belongsToMany('Videos', 'videos_languages', 'language_id', video_id');
	}
}

Then in code to loop through videos and languages

videos = Video::where('approved', '=', 1)->orderBy('id')->get();

foreach($videos as $video) {
  var_dump($video->id);
  var_dump($video->name);
  foreach($video->languages as $lan) {
    var_dump($lan->name);
  }
}
Last updated 2 years ago.
0

Hey, thanks you two. The last solution is interesting, I think that is the right way :).

Last updated 2 years ago.
0

Hey, I've used the second solutions and it works, but now I'm faced with the following problem:

I want to get access only to a special pivot row. I've now added this to the Video model:

public function languages()
{
    return $this->belongsToMany('Language','videos_languages','video_id','language_id')->withPivot('titel','vl_id');
}

Some videos have more than 1 entry. If I edit the titel of a language, I only want to change the pivot entry with the matching 'vl_id'. Now I tried that one:

 $video = Video::find($id);

  $lang = $video->languages[0]->pivot->wherePivot('vl_id',Input::get('language_id'));

  $lang->pivot->titel = Input::get('titel');
  $lang->pivot->save();

My previous solutions was a foreach loop that checked all the pivot values with the needed one. But I want to get that pivot directly by addressing it via method. Is it possible?

Last updated 2 years ago.
0

I think I am understanding what you are trying. I might be wrong but I think you want to get at it from the other direction. Rather than getting the video and working your way to the language, start at the language instead.

I think I have your id names right, you might have to fix them up a bit... it's early here still and gross out ;)

$the_lang = Languages::where('id', '=', Input::get('language_id'))->wherePivot('video_id', '=', $id);
$the_lang->pivot->title = Input::get('title');
$the_lang->pivot->save();

I think that might be the easier way to get at it.... or am I misunderstanding the problem?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

dnish dnish Joined 24 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.