0

I'm trying to add the piece of code from How to get the id of youtube videos in laravel into my Controller.

Error:

Indirect modification of overloaded property App\Gallery::$link has no effect

GalleryController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Gallery;

class GalleryController extends Controller
{
    //
    public function galleryAll()
    {

       $gallery = Gallery::orderBy('date', 'desc')
        ->get()
        ->map(function ($gallery) {
            $gallery->link = parse_str( parse_url( $gallery->link, PHP_URL_QUERY ), $gallery->link );
            return view('gallery', ['gallery' => $gallery]);
        });


    }
}

I'm unsure on how to save it to the $gallery-link. I'm planning on doing a @foreach in the View, and I want all the $gallery-link to be modified to only contain the youtube video code.

Update:

Controller

class GalleryController extends Controller
{
    //
    public function galleryAll()
    {

       return Gallery::orderBy('date', 'desc')
        ->get()
        ->map(function ($gallery) {
            return view('gallery', ['gallery' => $gallery]);
        });


    }
}

Page gives me:

[{},{},{},{},{},{},{},{},{},{},{},{},{}]

Using

$gallery = Gallery::orderBy('date', 'desc')

gives me blank page.

Mitch M
  • 157
  • 11

2 Answers2

2

Returning the view should be the last part of your controller method.

map() basically takes the collection, loops through it and allows you to edit each iteration of the loop (each item in the collection) which in your case will be an instance of the Gallery model. So, what you're actually doing in you code is replacing each Gallery instance with an instance of Illuminate\View\View and then returning a collection of that.

As for the "Indirect modification of overloaded property" error you're getting have a look at: Indirect Modification of Overloaded Property Laravel MongoDB

I would suggest using an Accessor for this. In your Gallery model add the following:

public function getYoutubeIdAttribute()
{
    parse_str( parse_url( $this->link, PHP_URL_QUERY ), $query);

    return $query['v'];
}

Then your controller would just be something like:

public function galleryAll()
{
   $gallery =  Gallery::orderBy('date', 'desc')->get();

   return view('gallery', compact('gallery'));
}

And finally in your view you can simply access the youtube_id like:

@foreach($gallery as $g)

    {{ $g->youtube_link }}

@endforeach
Rwd
  • 34,180
  • 6
  • 64
  • 78
  • I'm confused on where the function getYoutubeIdAttribute() is called. If I call the Gallery::get() in the controller, does it automatically loop through all the functions in the model? Also, you say access it by $g->youtube_link. Where is youtube_link defined? Since we are using $g->link – Mitch M Jun 20 '18 at 14:34
  • "The accessor will automatically be called by Eloquent when attempting to retrieve the value of the first_name attribute:" > So, when a 'function' tries to access a value ($gallery->link), it'll use the 'function'? – Mitch M Jun 20 '18 at 14:37
  • Ahh. I've got it to work: Had to change $gallery->link to $this->link in the model. Thank you! – Mitch M Jun 20 '18 at 14:42
  • 1
    The accessor will be available in all of the Gallery models. Also, you won't need to loop through anything in your controller. Laravel uses php's magic methods quite a bit, so when you try and access a value on a model that doesn't exist it will check to see if there is an accessor for that attribute and if so call it. – Rwd Jun 20 '18 at 14:42
  • Ah, my bad. Sorry about that. I'm glad you got it working! – Rwd Jun 20 '18 at 14:43
-1

you can't using: $gallery->link = parse_str($str, $gallery->link)

parse_str($str, $output);//output array
because $gallery->link is string
you see:http://php.net/manual/en/function.parse-str.php
skipperhoa
  • 431
  • 3
  • 7
  • Still can't get it to work. I think I'm not returning the map/view correctly. When I remove all the code between ->map(function ($gallery) { } except the return view my page is blanco. – Mitch M Jun 19 '18 at 16:18
  • I've updated my question. It actually returns me brackets with no value's. – Mitch M Jun 19 '18 at 16:19