Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to echo strings and variable in laravel 5.1 blade

Tags:

php

laravel

I am using this code inside my PHP file:

university.blade.php

{{ url( $universities->universityName.'/students') }}" >

The problem is that displays just http://localhost:8000/students, not the value of variable. So what is the solution?

like image 314
Sajjad Avatar asked Nov 12 '15 07:11

Sajjad


2 Answers

Probably $universities->universityName is empty

Lets assume your controller looks like this:

<?php namespace App\Http\Controllers

use App\University; // I assume this is your model

class UniversityController extends Controller{

     public function index()
     {
         $universities = Univeristy::all();

         return view('universities', compact('universities'));
     }

}

Then later in your universities.blade.php

<a href="{!! url("{$univerisity->universityName}/students") !!}">Students</a>

or you can check if $universities->universityName is empty, if it doesn't print the URL means its empty.

@if(!$universities || !$universities->universityName)
    <a href="{!! url("{$univerisity->universityName}/students") !!}">Students</a>
@endif
like image 99
Emeka Mbah Avatar answered Sep 19 '22 20:09

Emeka Mbah


As it seems you're passing some parameter in the url.

I assume that you want to print those parameter in your view

Here i wrote an example route, controller with view for your understanding.

Step 1 : Write a route method

Route::get('get/{id}', 'publicController@get');

Step 2 : Write a function inside your controller

public function get($id)
    {
        return view('get')->with('id', $id);
    }

Now you're returning the parameter that you passed to the view

Step 3 : Show in your View

Inside your view you can simply echo it using

{{ $id }}

So If you have this in your url

http://localhost/yourproject/get/14

Then it will print 14

Hope this helps you

like image 26
Sulthan Allaudeen Avatar answered Sep 20 '22 20:09

Sulthan Allaudeen