如何在其他页面上编辑时在其他表格上获取图像

时间:2014-09-11 20:53:51

标签: php database laravel

1)我正在尝试在用户编辑请愿时获取一个图像,我有两个表请愿和用户 它是用户图像,我想保存在用户表的profile_img_url属性

这是我在AdminPetitonController文件中的post_edit方法。

2)如何在这里使用急切加载,我的意思是只使用       $ SponceringUser而不是$ ProfileUser。所以我不需要跑两个       单独查询。

 public function post_edit($id = null)
{
    if ($id)
    {
        $Petition = Petition::find($id);

        if (!$Petition)
        {
            //oh noes! invalid petition specified!
            Alert::error("That petition no longer exists");
            return Redirect::action('AdminPetitionsController@index');
        }
    }
    else
    {
        $Petition = new Petition;
    }

    $PetitionCreationForm = new AdminPetitionCreationForm;
    $errors = array();

    if ($PetitionCreationForm->passes())
    {
        $Petition->call_to_action = Input::get('call_to_action');

        if (empty($Petition->id))
        {
            $Petition->slug = Str::slug($Petition->call_to_action);
        }

        $Petition->recipient = Input::get('recipient');


        if (Input::get('feature_type') == '1')
        {
            $Petition->featured_sort_order = Input::get('featured_sort_order') + 1;
            $Petition->flag_featured = 1;
        }
        else if(Input::get('feature_type') == '0')
        {
            $Petition->featured_sort_order=null;
            $Petition->flag_featured = 0;
        }
        //$selected_position = Input::get('dropdown_menu_list');
        $Petition->description_md = Input::get('description_md');
        $Petition->description = Petition::parseMD($Petition->description_md);

        $Petition->letter_md = Input::get('letter_md');
        $Petition->letter = Petition::parseMD($Petition->letter_md);

        $Petition->target_signatures = Input::get('target_signatures', 50000);
        $Petition->flag_published = Input::get('flag_published');

        $Petition->media_type = Input::get('media_type', null);

        if (Input::get('media_type') == 'img' && Input::hasFile('petition_image'))
        {
            $Petition->media_url = Petition::uploadFile(Input::file('petition_image'));
        }
        else if (Input::get('media_type') == 'youtube' && Input::get('media_url_youtube'))
        {
            $Petition->media_url = Input::get('media_url_youtube');
        }


        //  how to fix this part .... gurrrrrrrrrrrr=======================
        $ProfileUser= $Petition->User();

        if (Input::get('profile_type') == 'image' && Input::hasFile('profile_image'))
        {
            $ProfileUser->profile_img_url =  Petition::uploadFile(Input::file('profile_image'));
        }
        else if (Input::get('profile_type') == 'url' && Input::get('profile_url'))
        {
            $ProfileUser->profile_img_url = Input::get('profile_url');
        }
        //$Petition->sponsor_user_id = $SponsoringUser->id;
        $ProfileUser->save();
        //====================================================

        try {
            try {
                $SponsoringUser = User::where('email', Input::get('user.email'))->firstOrFail();
            }
            catch (Exception $e)
            {
                $PetitionSponsorForm = new AdminPetitionSponsorForm(Input::get('user'));

                if ($PetitionSponsorForm->passes())
                {
                    $SponsoringUser = new User;
                    $SponsoringUser->email = Input::get('user.email');
                    $SponsoringUser->first_name = Input::get('user.first_name');
                    $SponsoringUser->last_name = Input::get('user.last_name');
                    $SponsoringUser->populateLocation(Input::get('user.zip'));

                    $SponsoringUser->save();
                }
                else
                {
                    throw new Exception();
                }
            }
            $Petition->save();

如何特别修复那部分(我突出显示)我想我只是弄乱了那一部分。 它给了我错误,保存是未定义的方法,我不确定我是否需要像$ ProfileUser那样制作其他变量。

有关详细信息,请点击:https://gist.github.com/gupta2205/b33dcf762876e5df34d9

1 个答案:

答案 0 :(得分:1)

您的Gist不清楚,但您是否在请愿模式中设置了用户关系?

如果是这样,请尝试将相关行更改为:

$ProfileUser= $Petition->User;
相关问题