Laravel background-image:url is returning 1 - php

I'm making a webapplication in Laravel. I have the following code snippet:
<div class="user-info" style="background-image:url({{ asset(Auth::user()->partner->background) or '/images/default/background1.jpg'}})">
This is the result in the HTML:
<div class="user-info" style="background-image:url(1)">
What I'm supposed to get is the following from the database:
images/backgrounds/HRVl7TXkAxlhASj14vAV.png
This is weird because if I do the following:
{{dd(Auth::user()->partner->background)}}
it does actually dd() the filename images/backgrounds/HRVl7TXkAxlhASj14vAV.png from the database. Why does it echo 1 istead of the filename when I put it in a background-image:url?

Try
{!! "'".asset(Auth::user()->partner->background)."'" or '/images/default/background1.jpg'!!}
EDIT
A bit messy, but should work
{!! Auth::user()->partner && Auth::user()->partner->background ? "'".asset(Auth::user()->partner->background)."'" : '/images/default/background1.jpg' !!}
I recommend you to write separate method in User for this feature.
Something like this:
public function background()
{
// your awesome logic
return $pathToBackground;
}
and then:
<div class="user-info" style="background-image:url('{!! asset(Auth::user()->background()) !!}');" >

try
<div class="user-info" style="background-image:url({{ asset(($background=Auth::user()->partner->background) ? $background :'/images/default/background1.jpg' )}})">

Related

Load data in view in Laravel

I have a simple controller function that fetch all records from db. but when i am trying to show all these records it show nothing. In fact it shows me hard coded foreach loop like this.
#foreach ($compactData as $value) {{ $value->Name }} #endforeach
this is my contoller function.
public function showallProducts()
{
$productstock = Product::all()->stocks;
$productoldprice = Product::all()->OldPrices;
$productcurrentprice = Product::all()->CurrentPrice;
$compactData=array('productstock', 'productoldprice', 'productcurrentprice');
return view('welcome', compact($compactData));
}
this is my view
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="title m-b-md">
All products
</div>
<table>
<tbody>
#foreach ($compactData as $value)
{{ $value->Name }}
#endforeach
</tbody>
</table>
</div>
</div>
</body>
why it is behaving like this. any solution?? I am using phpstorm version 17. Is their any setting issue to run project because what ever project I ran it gives me the only page which i ran with only html?
My route is.
Route::get('/', function () {
$action = 'showallProducts';
return App::make('ProductController')->$action();
});
Have you checked your $compactData variable? Please dd($compactData) to see what it contains.
Problem 1
You are accessing a relational property as a property of Eloquent collection, like this:
Product::all()->stocks
which is not correct. Because the Collection object doesn't have the property stocks but yes the Product object might have a stocks property. Please read the Laravel documentation about Collection.
Problem 2
$compactData = array('productstock', 'productoldprice', 'productcurrentprice');
This line creating an array of 4 string, plain string not variable. So, your $compactData is containing an array of 4 string. If you want to have a variable with associative array then you need to do the following:
$compactData = compact('productstock', 'productoldprice', 'productcurrentprice');
Problem 3
return view('welcome', compact($compactData));
Here you are trying to pass the $compactDate to the welcome view but unfortunately compact() function doesn't accept variable but the string name of that variable as I have written in Problem 2. So, it should be:
return view('welcome', compact('compactData'));
Problem 4
Finally, in the blade you are accessing each element of the $compactData data variable and print them as string which might be an object.
You most likely have a problem with your web server.
Try to use Laravel Valet as development environnement.
Edit : I found this : Valet for Windows
I think you didn't mention the blade in the name of the view file by which it is saved. So change the name of the file by which it is save to something like:
filename.blade.php
and try again.
Explanation:
#foreach ($compactData as $value) this is the syntax of blade template engine, and to parse and excute it, you have to mention the blade extension in the name.

How to encrypt GET url for search in Laravel 5.5?

I am getting stuck on this problem, I don't know hot to encrypt the URL for search in Laravel 5.5... the result like this :
localhost:8000/Akademik/Mahasiswa?cari=some_keyword
but I want like this :
localhost:8000/Akademik/Mahasiswa?cari=some_encrypted_keyword
like :
localhost:8000/Akademik/Mahasiswa?cari=Kas6F8ajhasdhhfbdgshek
this my MahasiswaController.php
public function index(Request $request)
{
if ($request->get('cari') == null) {
$datas = Mahasiswa::paginate(10);
return view('Akademik.Mahasiswa.mahasiswaIndex', compact('datas'))->with('no',($request->input('page',1)- 1)*10);
} else {
$cari = $request->get('cari');
$datas = Mahasiswa::where('nama','LIKE','%'.$cari.'%')->paginate(10);
return view('Akademik.Mahasiswa.mahasiswaIndex', compact('datas'))->with('no',($request->input('page',1)- 1)*10);
}
}
This my route/web.php
Route::Resource('Akademik/Mahasiswa','Akademik\Mahasiswa\MahasiswaController');
and This my mahasiswaIndex.blade.php (search form)
<div class="col s4 m6 right">
{{ Form::open(array('url' => 'Akademik/Mahasiswa','method' => 'get')) }}
<div class="row">
<div class="input-field col s12">
{{ Form::text('cari',null,['id' => 'cari','class' => 'col s12']) }}
<label for="cari">Cari</label>
</div>
</div>
{{ Form::close() }}
</div>
if you want to encrypt your input field. you must do in javascript/jquery AJAX before Sending the keyword to url result. let assume you have controller and route to make encryption like this :
localhost:8000/Akademik/encrypt
after that, you will get the keyword encrypted on var some_encrypted_keyword then send again via Ajax GET to url :
localhost:8000/Akademik/Mahasiswa?cari=some_encrypted_keyword
ask me anything. hope this is solve your problem
Are you sure it is actually what you need? For security considerations HTTPS protocol is used, which encrypts all the communication between client side and the server. If you simply want to hide raw data from your browser address bar, you might you POST method instead of GET.
You can encrypt your url parameter and decrypt it in your controller. You can try this:
In your view: Suppose your parameter is cari or more parameter you can encrypt.
<?php
$parameter =[
'cari' => (value of input field),
];
$parameter= Crypt::encrypt($parameter);
?>
a link
Your route will be:
Route::get('/url/{parameter}', 'YourController#methodName');
In your controller, You can decrypt your parameter:
public function methodName($cari){
$data = Crypt::decrypt($cari);
}
You must be use Crypt namespace in your top of controller as
use Illuminate\Support\Facades\Crypt;
Note: You can encrypt url parameter with Crypt::encrypt($parameter) and decrypt with Crypt::decrypt($parameter)

Laravel get div without ajax

Hi I am trying to get the content within a div element that also happens to be within a form into my controller. I dont want to use ajax. How may I get that done ?
<div id="editorcontents" name="editorcontents">
</div>
Then in controller
Use Input;
$content = Input::get('editorcontents');
In your controller, do something like this. Look up the correct way in the docs (https://laravel.com/docs/5.4/eloquent#retrieving-models). For example, if you want ALL input, you would do Input::all();, instead of Input::where('editorcontents')->get();
public function index() {
$content = Input::where('editorcontents')->get();
return view('your_view.blade.file', compact('content'));
}
Then in your view your would now have $content, that you passed from your controller.
start of by looking at it, add this at top of your view: {{ dd($content) }}. This will die dump $content.
Go ahead and remove that line and do something like (docs here https://laravel.com/docs/5.4/blade#loops):
<div id="editorcontents" name="editorcontents">
#forelse ($content as $value)
<li>{{ $value->body }}</li>
#empty
<p>No content</p>
#endforelse
</div>

pagination in Laravel 5

I'm having some trouble implementing pagination in Laravel 5. Though probably not what you would think.
I can get everything to work but can't understand how to get it to pull the correct information on the second page.
Here is what I have:
Routes:
Route::resource('find-a-wp-theme', 'searchController');
Controller:
public function store(){
$themes['themes'] = Fulls::where("tags", "like", "%".$_POST['theme']."%")->orwhere("tags", "like", "%".$_POST['free']."%")->orwhere("title", "like", "%".$_POST['free']."%")->paginate(12);
return view('find-a-wp-theme', $themes);
}
View:
#if($themes)
#foreach($themes as $theme)
{{$theme['title']}}<br />
<img style="width:250px;" src="{{$theme['image']}}"><br />
#endforeach
{!! $themes->render() !!}
#endif
Up till here everything works just fine. My problem is that when I click the paginating button it takes me to mypage?page=2 which actually takes me to the resource index() and I can't understand how to implement this correctly.
Any help would be much appreciated!
Look at append in the docs. http://laravel.com/docs/5.0/pagination
If your trying to pass variables in your URL you need to use appends().
<?php echo $users->appends(['sort' => 'votes'])->render(); ?>

Building an ecommerce site using Laravel: How do I view/route products based on their ID?

I followed a tutorial on Tutsplus about creating an ecommerce website using Laravel. The problem I'm having right now is when trying to route to a subfolder. In the tutorial, the instructor included a feature where you can view products by ID. And this is how he did it:
// StoreController.php
public function getView($id) {
return View::make('store.view')->with('store', Store::find($id));
}
This piece of code seems to be passing an id from the stores table. I think when a product is clicked, that's when the id is passed
// Routes.php
Route::controller('store', 'StoreController');
Also some of the templates:
// store\index.blade.php
<h2>Stores</h2>
<hr>
<div id="stores row">
#foreach($stores as $store)
<div class="stores col-md-3">
<a href="/store/products/view/{{ $store->id }}">
{{ HTML::image($store->image, $store->title, array('class' => 'feature', 'width'=>'240', 'height' => '127')) }}
</a>
<h3>{{ $store->title }}</h3>
<p>{{ $store->description }}</p>
</div>
#endforeach
</div><!-- end product -->
So.. How it goes is when I click on a product, it leads me to domain:8000/store/view/6 where 6 is the id.
This works fine but what I want to know is how do I route through a subfolder? Let's say I want it to be like this: store/view/products/6 considering that I have a folder called products and my view.blade.php is inside that like this: store/products/view.
In my StoreController class, I tried changing this
public function getView($id) {
return View::make('store.view')->with('store', Store::find($id));
}
to this
public function getView($id) {
return View::make('store.product.view')->with('store', Store::find($id));
}
but it does not seem to work giving me nothing but a Controller Method Not Found Error.
First, the view name View::make('store.product.view') has nothing to do with the URL.
You have to change the route:
Route::controller('store/view', 'StoreController');
And then adjust the name of your method in the controller because it should be the same as the segment of the URL after store/view
public function getProducts($id) {
return View::make('store.product.view')->with('store', Store::find($id));
}
I strongly recommend you read the Laravel docs on the topic

Categories