laravel 根据 id 在页面上显示数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44415669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:05:59  来源:igfitidea点击:

laravel display data on a page based on id

laravellaravel-5bladelaravel-blade

提问by Przemek

I have a page that shows links with name of businesses that are retrieved in database like this:

我有一个页面,其中显示了在数据库中检索到的企业名称链接,如下所示:

Controller:

控制器:

public function viewBusiness() {
// Return our "website" object
$business = Business::all();
// Pass the contents of the "html" property to the view
return view('viewBusiness', ['business' => $business]);
}

View:

看法:

@extends('master') @section('title', 'Live Oldham') @section('content')
@section('content')
            @foreach ($business as $businesses)
        <a target="_blank" href="{{ url('business/' . $businesses->name) }}"> {{($businesses->name) }}
        </a> @endforeach       
@endsection

Route:

路线:

Route::get('business/list', 'BusinessController@viewBusiness')->name('viewBusiness');

I then have added a function where user click on a link and it is taken to a page which displays all data for that specific business, however it diplays all data but for all businesses.

然后我添加了一个功能,用户点击一个链接,它被带到一个页面,该页面显示该特定业务的所有数据,但它显示所有数据,但不包括所有业务。

Controller:

控制器:

function displayBusiness() {
    $business = Business::all();
    $address = Address::all();
    return view('displayBusiness', ['business' => $business, 'address' => $address]);
}

View:

看法:

@foreach ($business as $businesses)
    <p>{{$businesses->name}}</p>
    <p>{{$businesses->email}}</p>
@endforeach
@foreach ($address as $addresses)
    <p>{{$addresses->firstline_address}}</p>
    <p>{{$addresses->secondline_address}}</p>
    <p>{{$addresses->town}}</p>
    <p>{{$addresses->city}}</p>
    <p>{{$addresses->postcode}}</p>
    <p>{{$addresses->telephone}}</p>
@endforeach

Route:

路线:

Route::get('business/{name}', 'BusinessController@displayBusiness')->name('displayBusiness');

Now here's the question, how can this code be modified so only a business that match either bussiness->name or business->id is displayed. (I guess name is taken when user clicks on a name.

现在的问题是,如何修改此代码,以便仅显示与 business->name 或 business->id 匹配的企业。(我猜当用户单击名称时会使用名称。

Another question is how to restrict the url so that if localhost/business/{name} is not equal to any business->name in the database returns error? at the moment it shows the page no matter what you enter.

另一个问题是如何限制 url 以便如果 localhost/business/{name} 不等于数据库中的任何 business->name 返回错误?目前,无论您输入什么,它都会显示该页面。

Thanks!

谢谢!

回答by Smo

I do not know if I understood the question, but that may be the beginning of a solution ...

我不知道我是否理解这个问题,但这可能是解决方案的开始......

First view :

第一个观点:

@extends('master') @section('title', 'Live Oldham')
@section('content')
            @foreach ($business as $businesses)
        <a target="_blank" href="{{ url('business/' . $businesses->id) }}"> {{($businesses->name) }}
        </a> @endforeach       
@endsection

Second Controller :

第二控制器:

function displayBusiness($id) {
    $business = Business::find($id);
    $address = Address::find($id);
    return view('displayBusiness', compact('business', 'address'));
}

Second View :

第二个观点:

    <p>{{$business->name}}</p>
    <p>{{$business->email}}</p>
    <p>{{$address->firstline_address}}</p>
    <p>{{$address->secondline_address}}</p>
    <p>{{$address->town}}</p>
    <p>{{$address->city}}</p>
    <p>{{$address->postcode}}</p>
    <p>{{$address->telephone}}</p>

Second Route :

第二条路线:

Route::get('business/{id}', 'BusinessController@displayBusiness')->name('displayBusiness');

回答by Jerodev

Route parameters are available in the controller function as parameters. Now you can build a query with this function. If your query does not return any results, you can send the user back to the business overview.

路径参数在控制器功能中作为参数提供。现在您可以使用此函数构建查询。如果您的查询未返回任何结果,您可以将用户发送回业务概览。

function displayBusiness($name) {
    $business = Business::where('name', $name)->orWhere('id', $name)->first();
    if ($business === null)
    {
        // No business with this name or id found.
        // Redirect to businesses list page.
    }

    $address = Address::all();
    return view('displayBusiness', ['business' => $business, 'address' => $address]);
}