duanlachu7344 2018-06-19 15:47
浏览 285
已采纳

Laravel使用map()并返回视图+获取youtube视频ID

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.

  • 写回答

2条回答 默认 最新

  • douwen2158 2018-06-19 21:47
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计