Laravel不返回页面

I am saving data into database by passing variable throught ajax and using it in the controller.

Saving works fine, it saves data into a database but the problem is displaying saved page. Database has 2 fields, name and html. Name is obviously name of the website and html is pure html code. so when user saves page with name 'random' I want to display html page to him using localhost/random

Error message I get is:

Trying to get property of non-object (View: C:\xampp\htdocs\fyprojectesources\views\layouts\website.blade.php)

Here's what I have done so far:

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");
        });
}

Controller:

     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'));
}
}

Routes:

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

What is going on?

2个回答


问题是当您需要传递$ website时,您正在将名为name的变量传递给视图。 此外,您的口才查询有误。</ p>

尝试更改以下内容,</ p>

  $ name-> html //更改此
$ website-> html //至此
返回视图(“布局/网站”,紧凑型(“名称”)); //更改
返回视图(“布局/网站”,紧凑型(“网站”)); //为此
$ website =网站:: where('name',$ name)-> first()-> html; //更改
$ website =网站:: where('name',$ name)-> first(); //为此
</ code> </ pre>
     </ div>

展开原文

原文

The problem is you are passing variable called name to your view while you need to pass $website. furthermore you have a mistake in your Eloquent query.

Try to change the following,

$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


首先,您无需通过postDB进行重定向,而必须通过javascript </ p>进行重定向

控制器</ strong> </ p>

 公共函数postDB(Request $ request){

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

     返回response()-> json(['url'=> url($ website-> name)]);
}
</ code> </ pre>

JavaScript </ strong> </ p>

  $ .post('http:// localhost / template',{
     _token:$('meta [name = csrf-token]')。attr('content'),
     newCode :(代码2),
     name_website:(web_name),
},功能(响应){
     //在此重定向到新页面。
     window.location = response.url;
})
.done(function(){

})
.fail(function(){
     alert(“错误”);
});
</ code> </ pre>

控制器</ strong> </ p>

 公共功能网站($ name){
     $ website =网站:: where('name',$ name)-> first();
     返回视图(“布局/网站”,紧凑型(“网站”));
}
</ code> </ pre>

现在,您可以通过访问 $ website </ strong>变量(例如 $ website-> html </ strong> </ p>)来访问html。
     </ div>

展开原文

原文

First instead of redirecting on postDB, you have to redirect via javascript

Controller

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");
});

Controller

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

Now you can access the html by accessing the $website variable like $website->html

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐