mercredi 7 juin 2017

laravel display data on a page based on id

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=""> 
        </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></p>
    <p></p>
@endforeach
@foreach ($address as $addresses)
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></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.

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.

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire