Laravel没有返回页面

时间:2017-02-10 14:26:52

标签: ajax laravel axios

我通过在ajax中传递变量并在控制器中使用它来将数据保存到数据库中。

保存工作正常,它将数据保存到数据库中,但问题是显示已保存的页面。数据库有2个字段,名称和html。名称显然是网站的名称,而html是纯HTML代码。因此,当用户保存名称为“随机”的页面时,我想使用localhost / random

向他显示html页面

我得到的错误信息是:

  

尝试获取非对象的属性(查看:C:\ xampp \ htdocs \ fyproject \ resources \ views \ layouts \ website.blade.php)

这是我到目前为止所做的事情:

的Ajax:

var web_name;
function updateDatabase(newCode, name_website)
{
    code2 = document.getElementById("content-link2").innerHTML;
    web_name = ($('#website_name').val());
    // make an ajax request to a PHP file
    // on our site that will update the database
    // pass in our lat/lng as parameters
    $.post('http://localhost/template', {
            _token: $('meta[name=csrf-token]').attr('content'),
            newCode: (code2),
            name_website: (web_name),
        })
        .done(function() {
        })
        .fail(function() {
            alert("error");
        });
}

控制器:

     public function postDB(Request $request) {
        $newName = $request->input('name_website');
        $newLat = $request->input('newCode');
        $websites = new Website();
        $websites->name = $newName;
        $websites->html = $newLat;
        $websites->save();
        $name = $newName;
        return redirect($name);
    }
    public function website($name) {
    $website = Website::where('name', $name)->first()->html;

    // Render resources/views/template.blade.php or any view you want
    // and pass the data. E.g. $website, so you can access $website->html in your view.
    return view('layouts/website', compact('name'));
}
}

路线:

Route::get('home', 'BuilderController@homepage');
Route::get('template', 'BuilderController@templates');
Route::post('template', 'BuilderController@postDB');
Route::get('logout', 'BuilderController@getLogout');
Route::get('/{name}', 'BuilderController@website');

website.blade.php:

@extends('layouts.master') @section('title', 'Website Builder') @section('content')
<meta name="csrf-token" content="{{ csrf_token() }}" />


{{html_entity_decode($name->html)}}
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.get('/localhost/name').then(
        html => document.querySelector('html').innerHTML = html     
    );
</script>
</html>
@endsection @show

发生了什么事?

2 个答案:

答案 0 :(得分:0)

问题是,当您需要传递$ website时,您将名为name的变量传递给您的视图。此外,您的Eloquent查询中存在错误。

尝试更改以下内容,

$name->html // Change this
$website->html // to this
return view('layouts/website', compact('name')); // Change this
return view('layouts/website', compact('website')); // To this
$website = Website::where('name', $name)->first()->html; // Change this
$website = Website::where('name', $name)->first(); // To this

答案 1 :(得分:0)

首先不是在postDB上重定向,而是必须通过javascript重定向

<强>控制器

public function postDB(Request $request) {

    $website = new Website();
    $website->name = $request->input('name_website');
    $website->html = $request->input('newCode');
    $website->save();

    return response()->json(['url' => url($website->name)]);
}

<强>的Javascript

$.post('http://localhost/template', {
    _token: $('meta[name=csrf-token]').attr('content'),
    newCode: (code2),
    name_website: (web_name),
}, function(response){
    // Redirecting to the new page here.
    window.location = response.url;
})
.done(function() {

})
.fail(function() {
    alert("error");
});

<强>控制器

public function website($name) {
    $website = Website::where('name', $name)->first();
    return view('layouts/website', compact('website'));
}

现在您可以访问 $ website 变量来访问html,例如 $ website-&gt; html