{"id":22187,"date":"2020-10-06T17:36:39","date_gmt":"2020-10-06T17:36:39","guid":{"rendered":"http:\/\/associating_media_files_with_laravel_s_eloquent_models"},"modified":"2024-06-03T13:06:19","modified_gmt":"2024-06-03T20:06:19","slug":"associating_media_files_with_laravel_s_eloquent_models","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models","title":{"rendered":"Associating Media Files With Laravel\u2019s Eloquent Models"},"content":{"rendered":"<div class=\"wp-block-cloudinary-markdown \"><p><a href=\"https:\/\/cloudinary.com\/blog\/introducing_cloudinary_s_new_laravel_sdk\">Laravel<\/a>, currently the most popular PHP framework, offers databases an efficient, well-written Active Record implementation, called the Eloquent Object-Relational Mapper (ORM). Specifically, Eloquent maps a database table to an Eloquent model along with fluent methods and expressions for querying and modifying the database\u2019s records.<\/p>\n<p>Given that you as web developers routinely and frequently upload, download, and transform media files, being able to efficiently associate them with your Laravel Eloquent models saves time and resources. This tutorial describes that process, step by step, with code examples.<\/p>\n<h2>Set Up a Laravel Project<\/h2>\n<ol>\n<li>\n<p>Install <a href=\"https:\/\/getcomposer.org\/\">Composer<\/a> and <a href=\"https:\/\/www.php.net\/manual\/en\/install.php\">PHP<\/a> on your development or production machine and then run this command:<\/p>\n<pre class=\"js-syntax-highlighted\"><span><code class=\"hljs shcb-wrap-lines\">composer create-project --prefer-dist laravel\/laravel project\n<\/code><\/span><\/pre>\n<\/li>\n<li>\n<p>Go to the <code>project<\/code> directory and rename the <code>env.example<\/code> file to <code>.env<\/code>.<\/p>\n<\/li>\n<li>\n<p>Run the project with the command <code>php artisan serve<\/code>.<\/p>\n<\/li>\n<\/ol>\n<p>Your Laravel project is now up and running.<\/p>\n<h2>Set Up Cloudinary\u2019s Laravel SDK<\/h2>\n<ol>\n<li>\n<p>Install <a href=\"https:\/\/github.com\/cloudinary-labs\/cloudinary-laravel#installation\">Cloudinary\u2019s Laravel SDK<\/a>:<\/p>\n<pre class=\"js-syntax-highlighted\"><code>composer require cloudinary-labs\/cloudinary-laravel\n<\/code><\/pre>\n<p><strong>Note:<\/strong> Be sure to follow the steps in the #Installation section. Publish the configuration file and add your Cloudinary credentials to the <code>.env<\/code> file of your app.<\/p>\n<\/li>\n<li>\n<p>Publish the migration file in Cloudinary\u2019s Laravel SDK with this command:<\/p>\n<pre class=\"js-syntax-highlighted\"><code>php artisan vendor:publish --provider=&quot;CloudinaryLabs\\CloudinaryLaravel\\CloudinaryServiceProvider&quot; --tag=&quot;cloudinary-laravel-migration&quot;\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Run <code>php artisan migrate<\/code> to create the required media table in your database. Verify that the table is in place afterwards.<\/p>\n<p>Before uploading files to Cloudinary, <a href=\"https:\/\/cloudinary.com\/users\/register_free\">sign up<\/a> for a free Cloudinary account, log in, and note your cloud name and API keys from the dashboard.<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/image\/upload\/w_700,c_fill,f_auto,q_auto,dpr_2.0\/Web_Assets\/blog\/media_library.png\" alt=\"media library\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"1400\" height=\"899\"\/><\/p>\n<h2>Set Up the Mechanics for Attaching Media<\/h2>\n<p>Follow the two steps below to attach media to a webpage, which can accept as many as you desire as thumbnails or as files. Follow these two steps:<\/p>\n<ol>\n<li>\n<p>Create with Laravel migrations a <code>pages<\/code> table with two fields, <code>id<\/code> and <code>body<\/code>, in your database.<\/p>\n<\/li>\n<li>\n<p>Create the <code>Page<\/code> model that maps to the <code>pages<\/code> database table:<\/p>\n<\/li>\n<\/ol>\n<pre class=\"js-syntax-highlighted\"><code>&lt;?php\n\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Page extends Model\n{\n\n    \/**\n    * The table associated with the model.\n    *\n    * @var string\n    *\/\n    protected $table = 'pages';\n}\n<\/code><\/pre>\n<h2>Set Up the Mechanics for Attaching Files to Laravel Eloquent Models<\/h2>\n<p>Next, import the <code>CloudinaryLabs\\CloudinaryLaravel\\MediaAlly<\/code> trait into your model so that you can attach media files to the Eloquent model:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">App<\/span>;\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Database<\/span>\\<span class=\"hljs-title\">Eloquent<\/span>\\<span class=\"hljs-title\">Model<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">CloudinaryLabs<\/span>\\<span class=\"hljs-title\">CloudinaryLaravel<\/span>\\<span class=\"hljs-title\">MediaAlly<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Page<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Model<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">MediaAlly<\/span>;\n\n    ...\n}\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p><em>App\\Models\\Page.php<\/em><\/p>\n<h2>Attach, Retrieve, Or Delete Media Files<\/h2>\n<p>You can now attach media files to the <code>Page<\/code> model in your Laravel controller. Below are three use cases. The first two assume that you\u2019re uploading the files as attachments <strong>for the first time<\/strong><\/p>\n<h3>First Use Case<\/h3>\n<p>Create a page and then attach files to it:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">...\n$page = Page::create(<span class=\"hljs-keyword\">$this<\/span>-&gt;request-&gt;input());\n$page-&gt;attachMedia($file);   <span class=\"hljs-comment\">\/\/ Example of $file is $request-&gt;file('file');<\/span>\n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>To verify, check the <code>media<\/code> table in your database for a new record that looks like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/image\/upload\/c_fill,f_auto,q_auto\/Web_Assets\/blog\/media-table-ss.png\" alt=\"media table\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"1521\" height=\"133\"\/><\/p>\n<h3>Second Use Case<\/h3>\n<p>Fetch an existing page and attach media files to it:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">...\n$page = Page::find(<span class=\"hljs-number\">2<\/span>);\n$page-&gt;attachMedia($file);   <span class=\"hljs-comment\">\/\/ Example of $file is $request-&gt;file('file');<\/span>\n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h3>Third Use Case<\/h3>\n<p>In this instance, you\u2019ve already uploaded to Cloudinary or another cloud storage the media files to be attached to a page. Call the <code>attachRemoteMedia<\/code> method:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">...\n$page = Page::create(<span class=\"hljs-keyword\">$this<\/span>-&gt;request-&gt;input());\n$page-&gt;attachRemoteMedia($existingRemoteUrl); \n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">...\n$page = Page::find(<span class=\"hljs-number\">2<\/span>);\n$page-&gt;attachRemoteMedia($existingRemoteUrl);  \n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h3>File Retrieval<\/h3>\n<p>You can retrieve all the files you\u2019ve attached to the <code>Page<\/code> record, or just the first or last file.<\/p>\n<p><strong>To retrieve all the files that are attached to the <code>Page<\/code> record:<\/strong><\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">...\n$filesBelongingToSecondPage = Page::find(<span class=\"hljs-number\">2<\/span>)-&gt;fetchAllMedia();\n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p><strong>To retrieve the first file that\u2019s attached to the <code>Page<\/code> record:<\/strong><\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">...\n$fileBelongingToSecondPage = Page::find(<span class=\"hljs-number\">2<\/span>)-&gt;fetchFirstMedia();\n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p><strong>To retrieve the last file that\u2019s attached to the <code>Page<\/code> record:<\/strong><\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">...\n$fileBelongingToSecondPage = Page::find(<span class=\"hljs-number\">2<\/span>)-&gt;fetchLastMedia();\n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h3>File Deletion<\/h3>\n<p>To delete all the files you\u2019ve attached to the <code>Page<\/code> record:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">...\n$page = Page::find(<span class=\"hljs-number\">2<\/span>);\n$page-&gt;detachMedia();\n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h2>Leverage More Cloudinary Capabilities<\/h2>\n<p>Uploading and attaching files barely scratches the surface of media management. Cloudinary helps you administer the entire spectrum of your media\u2019s lifecycle, end to end, from upload and transformation to optimization and delivery. Do <a href=\"https:\/\/cloudinary.com\">check it out<\/a>.<\/p>\n<h2>Want to Learn More About Laravel?<\/h2>\n<ul>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/introducing_cloudinary_s_new_laravel_sdk\">Introducing Cloudinary\u2019s New Laravel SDK<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\">Laravel File Upload to a Local Server Or to the Cloud<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/compressing_resizing_and_optimizing_images_in_laravel\">Compressing, Resizing, and Optimizing Images in Laravel<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models\">Associating Media Files With Laravel\u2019s Eloquent Models<\/a>\n<\/li>\n<\/ul>\n<\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":41,"featured_media":22188,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[25,227,263],"class_list":["post-22187","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-asset-management","tag-performance-optimization","tag-sdk"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Associating Media Files With Laravel\u2019s Eloquent Models<\/title>\n<meta name=\"description\" content=\"Learn how to leverage Cloudinary&#039;s Laravel SDK to attach, retrieve, or delete media files to or from webpages in Laravel Eloquent models.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Associating Media Files With Laravel\u2019s Eloquent Models\" \/>\n<meta property=\"og:description\" content=\"Learn how to leverage Cloudinary&#039;s Laravel SDK to attach, retrieve, or delete media files to or from webpages in Laravel Eloquent models.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-06T17:36:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-03T20:06:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b-png?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"1540\" \/>\n\t<meta property=\"og:image:height\" content=\"847\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Associating Media Files With Laravel\u2019s Eloquent Models\",\"datePublished\":\"2020-10-06T17:36:39+00:00\",\"dateModified\":\"2024-06-03T20:06:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models\"},\"wordCount\":8,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA\",\"keywords\":[\"Asset Management\",\"Performance Optimization\",\"SDK\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2020\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models\",\"url\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models\",\"name\":\"Associating Media Files With Laravel\u2019s Eloquent Models\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA\",\"datePublished\":\"2020-10-06T17:36:39+00:00\",\"dateModified\":\"2024-06-03T20:06:19+00:00\",\"description\":\"Learn how to leverage Cloudinary's Laravel SDK to attach, retrieve, or delete media files to or from webpages in Laravel Eloquent models.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA\",\"width\":1540,\"height\":847},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Associating Media Files With Laravel\u2019s Eloquent Models\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Associating Media Files With Laravel\u2019s Eloquent Models","description":"Learn how to leverage Cloudinary's Laravel SDK to attach, retrieve, or delete media files to or from webpages in Laravel Eloquent models.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models","og_locale":"en_US","og_type":"article","og_title":"Associating Media Files With Laravel\u2019s Eloquent Models","og_description":"Learn how to leverage Cloudinary's Laravel SDK to attach, retrieve, or delete media files to or from webpages in Laravel Eloquent models.","og_url":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models","og_site_name":"Cloudinary Blog","article_published_time":"2020-10-06T17:36:39+00:00","article_modified_time":"2024-06-03T20:06:19+00:00","og_image":[{"width":1540,"height":847,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b-png?_i=AA","type":"image\/png"}],"twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models"},"author":{"name":"","@id":""},"headline":"Associating Media Files With Laravel\u2019s Eloquent Models","datePublished":"2020-10-06T17:36:39+00:00","dateModified":"2024-06-03T20:06:19+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models"},"wordCount":8,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA","keywords":["Asset Management","Performance Optimization","SDK"],"inLanguage":"en-US","copyrightYear":"2020","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models","url":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models","name":"Associating Media Files With Laravel\u2019s Eloquent Models","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA","datePublished":"2020-10-06T17:36:39+00:00","dateModified":"2024-06-03T20:06:19+00:00","description":"Learn how to leverage Cloudinary's Laravel SDK to attach, retrieve, or delete media files to or from webpages in Laravel Eloquent models.","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA","width":1540,"height":847},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/associating_media_files_with_laravel_s_eloquent_models#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Associating Media Files With Laravel\u2019s Eloquent Models"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":""}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649719810\/Web_Assets\/blog\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b\/Attaching-Media-Files-to-Laravel-Eloquent-Models-v1_1_22188a042b.png?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/22187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=22187"}],"version-history":[{"count":3,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/22187\/revisions"}],"predecessor-version":[{"id":34324,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/22187\/revisions\/34324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/22188"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=22187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=22187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=22187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}